一、单个神经元网络构建

一、本人使用编译器为Jupyter Notebook,tensorflow版本为1.13.1

import tensorflow as tf
print(tf.__version__)
"""
1.13.1
"""

二、训练单个神经元网络

x为-1.0, 0.0, 1.0, 2.0, 3.0, 4.0
y为-3.0, -1.0, 1.0, 3.0, 5.0, 7.0
人用大脑很容易可以看出y=2*x-1,这里进行构建单一神经元模型去预测x=10.0时,y的值,理论应该为19,接下来看下模型的表现吧

from tensorflow import keras
import numpy as np

model = keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])
model.compile(optimizer='sgd',loss='mean_squared_error')

x = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
y = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)

model.fit(x,y,epochs=1000)

model.predict([10.0])
"""
array([[18.999922]], dtype=float32)
"""

最终模型训练1000次,预测的结果为18.999922,与实际真实值19相差不太大

猜你喜欢

转载自blog.csdn.net/qq_41264055/article/details/125444653