【发布时间】:2020-10-19 19:25:17
【问题描述】:
我写了一个非常基本的张量流模型,我想预测一个数字:
import tensorflow as tf
import numpy as np
def HW_numbers(x):
y = (2 * x) + 1
return y
x = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0], dtype=float)
y = np.array(HW_numbers(x))
model = tf.keras.models.Sequential([tf.keras.layers.Dense(units=1,input_shape=[1])])
model.compile(optimizer='sgd',loss='mean_squared_error')
model.fit(x,y,epochs = 30)
print(model.predict([10.0]))
上面的代码工作正常。但是如果我在 Dense 层中添加一个激活函数,预测就会变得很奇怪。我试过'relu'、'sigmoid'、'tanh'等。
我的问题是,为什么会这样?激活函数究竟在搞乱预测的那个单层中做了什么? 我用过 TensorFlow 2.0
【问题讨论】:
标签: python tensorflow machine-learning neural-network activation-function