首页 > 其他 > 详细

tensorflow keras 线性回归

时间:2020-04-22 21:03:10      阅读:54      评论:0      收藏:0      [点我收藏+]

给一组数据点xs,ys 训练后预测另外一个x对应的y值
Keras的核心数据结构是“model”,model是一种组织网络层的方式。
Keras中主要的模型是Sequential模型,Sequential是一系列网络层按顺序构成的栈
dense表示全连接神经网络
units: 正整数,输出空间维度
inputshape表示输入张量的形状
损失函数使用mean_squared_error,优化器使用随机梯度下降法(sgd)
epochs训练轮数,会从初始参数开始使用sgd迭代使得loss减小

import tensorflow as tf
import numpy as np
from tensorflow import keras
# Define the training data
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
# Define the neural network model
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
# Configure the learning process
model.compile(optimizer=‘sgd‘, loss=‘mean_squared_error‘)
# train data
model.fit(xs, ys, epochs=100)
# predict
print(model.predict([10.0]))

tensorflow keras 线性回归

原文:https://www.cnblogs.com/jun-phy/p/12755562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!