tensorflow2 神经模型

120 阅读1分钟

二层神经模型 数据集advertising

第一层模型的维数必须和输入数据的维数相同

import pandas as pd
import tensorflow as tf
data = pd.read_csv(r'C:\Users\zp\Desktop\新建文本文档 (5).txt')
x = data.iloc[:,:-1]
y = data.iloc[:,-1]
#二层神经模型
model = tf.keras.Sequential(
    [tf.keras.layers.Dense(10,input_shape=(3,),activation='relu'),#第一层,10个神经元,输入数据为3维,以relu为激活函数
     tf.keras.layers.Dense(1)]#第二层 输出为1维
    )
model.compile(optimizer='adam',loss='mse')
model.fit(x,y,epochs=800)
#预测
test = data.iloc[-10:,:-1]
print(model.predict(test))

结果

[[10.534118 ]
 [ 7.598111 ]
 [ 6.253477 ]
 [19.597713 ]
 [17.233036 ]
 [ 4.2911315]
 [ 6.849201 ]
 [11.427596 ]
 [25.407898 ]
 [13.568398 ]]