【发布时间】:2017-06-13 20:00:19
【问题描述】:
我正在尝试使用tf.contrib.learn.estimator 在张量流中构建具有加权损失函数的神经网络。运行代码时,我总是遇到同样的错误。
这是估算器的模型:
def model_fn(features, targets, mode, params):
"""Model function for Estimator."""
# Connect the first hidden layer to input layer
# (features) with relu activation
first_hidden_layer = tf.contrib.layers.relu(features, 20)
# Connect the second hidden layer to first hidden layer with relu
second_hidden_layer = tf.contrib.layers.relu(first_hidden_layer, 20)
third_hidden_layer = tf.contrib.layers.relu(second_hidden_layer, 20)
# Connect the output layer to second hidden layer (no activation fn)
output_layer = tf.contrib.layers.linear(second_hidden_layer, 1)
# Reshape output layer to 1-dim Tensor to return predictions
predictions = tf.reshape(output_layer, [-1])
# Calculate loss weighting false negatives up
sess=tf.InteractiveSession()
t=tf.constant(0)
def weightedloss(prediction=[], target=[]):
losssum = 0.0
for x in range(len(prediction)):
if prediction[x] == 1 & target[x] == 0:
losssum += 1.0
elif prediction[x] == 0 & target[x] == 1:
losssum += 9.0
else:
losssum += 0.0
return tf.constant(losssum)
print(list(predictions.eval(session=sess)))
loss = weightedloss(list(predictions.eval(session=sess)), list(targets.eval(session=sess)))
# Calculate root mean squared error as additional eval metric
eval_metric_ops = {
"rmse":
tf.metrics.root_mean_squared_error(
tf.cast(targets, tf.float64), predictions)
}
train_op = tf.contrib.layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
learning_rate=params["learning_rate"],
optimizer="SGD")
return model_fn.ModelFnOps(
mode=mode,
predictions=predictions_dict,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops)
这是我在代码中使用该模型 fn 的方式:
nn = tf.contrib.learn.Estimator(model_fn=model_fn, params=.003)
print("reachedfit")
# Fit model.
#classifier.fit(x=x_train, y=y_train, steps=1000)
nn.fit(x=x_train, y=y_train, steps=1000)
print("reachedpredict")
y = list(nn.predict(x_test))
最后,这是我得到的错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input' with dtype double
[[Node: input = Placeholder[dtype=DT_DOUBLE, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
我做错了什么?
【问题讨论】:
-
reachedfit 和reacherpredict 都没有被打印出来。
标签: python numpy tensorflow