import numpy as np
import tensorflow as tf
X_p = tf.placeholder(tf.float32,[None,3] )
y_p = tf.placeholder(tf.float32, [None,1])
print(X_p)
x = [[1,2,3],[1,2,3]]
y = [[1],[2]]
weight = tf.Variable(tf.random_normal([3,1]))
model = tf.nn.sigmoid(tf.matmul(X_p,weight)+1)
error = tf.reduce_sum(y * tf.log(model))
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(error)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for x in range(100):
sess.run(optimizer, {X_p: x, y_p:y})
X_p的形状为[无,3],x为形状[2,3],y_p = [无,1],y = [2,1]
我真的不明白为什么占位符会停止numpy数组来获取数据。
答案 0 :(得分:2)
您遇到的问题是,您还要使用x
作为循环变量来覆盖x
变量。所以当你试图将x
传递给feed dict时,你传递的是循环变量,而不是你的张量。尝试将循环变量更改为其他内容,例如:
for i in range(100):
sess.run(optimizer, {X_p: x, y_p:y})