逻辑 'AND' 的 Tensorflow 自定义估算器
Tensorflow custom estimator for logical 'AND'
我正在尝试创建一个简单的 one-layer/one-unit 神经网络,其中包含能够计算逻辑 AND 运算的 tensorflow 自定义估计器,但我在使用 sigmoid 激活时遇到了问题——我想设置阈值
这是我的代码
x = np.array([
[0, 0],
[1, 0],
[0, 1],
[1, 1]
], dtype=np.float32)
y = np.array([
[0],
[0],
[0],
[1]
])
def sigmoid(val):
res = tf.nn.sigmoid(val)
isGreater = tf.greater(res, tf.constant(0.5))
return tf.cast(isGreater, dtype=tf.float32)
def model_fn(features, labels, mode, params):
predictions = tf.layers.dense(inputs=features, units=1, activation=sigmoid)
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
loss = tf.losses.sigmoid_cross_entropy(labels, predictions)
optimizer = tf.train.GradientDescentOptimizer(0.5)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
nn = tf.estimator.Estimator(model_fn=model_fn)
input_fn = tf.estimator.inputs.numpy_input_fn(x=x, y=y, shuffle=False, num_epochs=None)
nn.train(input_fn=input_fn, steps=500)
但这会引发错误
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'dense/kernel:0' shape=(2, 1) dtype=float32_ref>", "<tf.Variable 'dense/bias:0' shape=(1,) dtype=float32_ref>"] and loss Tensor("sigmoid_cross_entropy_loss/value:0", shape=(), dtype=float32).
我该如何解决这个问题?请帮忙..
我的另一个问题 - 为什么 Tensorflow 没有内置的 sigmoid 激活阈值?不就是二分类最需要的东西之一吗(withsigmoid/tanh)?
有一个built-in sigmoid激活函数,即tf.nn.sigmoid
。
然而,当你创建一个网络时,你不应该在最后一层使用激活。您需要向图层提供未缩放的逻辑,如下所示:
predictions = tf.layers.dense(inputs=features, units=1, activation=None)
loss = tf.losses.sigmoid_cross_entropy(labels, predictions)
否则,对于您的自定义 sigmoid,您的预测将是 0
或 1
,并且没有可用的梯度。
我正在尝试创建一个简单的 one-layer/one-unit 神经网络,其中包含能够计算逻辑 AND 运算的 tensorflow 自定义估计器,但我在使用 sigmoid 激活时遇到了问题——我想设置阈值
这是我的代码
x = np.array([
[0, 0],
[1, 0],
[0, 1],
[1, 1]
], dtype=np.float32)
y = np.array([
[0],
[0],
[0],
[1]
])
def sigmoid(val):
res = tf.nn.sigmoid(val)
isGreater = tf.greater(res, tf.constant(0.5))
return tf.cast(isGreater, dtype=tf.float32)
def model_fn(features, labels, mode, params):
predictions = tf.layers.dense(inputs=features, units=1, activation=sigmoid)
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
loss = tf.losses.sigmoid_cross_entropy(labels, predictions)
optimizer = tf.train.GradientDescentOptimizer(0.5)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
nn = tf.estimator.Estimator(model_fn=model_fn)
input_fn = tf.estimator.inputs.numpy_input_fn(x=x, y=y, shuffle=False, num_epochs=None)
nn.train(input_fn=input_fn, steps=500)
但这会引发错误
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'dense/kernel:0' shape=(2, 1) dtype=float32_ref>", "<tf.Variable 'dense/bias:0' shape=(1,) dtype=float32_ref>"] and loss Tensor("sigmoid_cross_entropy_loss/value:0", shape=(), dtype=float32).
我该如何解决这个问题?请帮忙..
我的另一个问题 - 为什么 Tensorflow 没有内置的 sigmoid 激活阈值?不就是二分类最需要的东西之一吗(withsigmoid/tanh)?
有一个built-in sigmoid激活函数,即tf.nn.sigmoid
。
然而,当你创建一个网络时,你不应该在最后一层使用激活。您需要向图层提供未缩放的逻辑,如下所示:
predictions = tf.layers.dense(inputs=features, units=1, activation=None)
loss = tf.losses.sigmoid_cross_entropy(labels, predictions)
否则,对于您的自定义 sigmoid,您的预测将是 0
或 1
,并且没有可用的梯度。