我刚接触tensorflow,并想训练一个用于分类的逻辑回归模型。
# Set model weights
W = tf.Variable(tf.zeros([30, 16]))
b = tf.Variable(tf.zeros([16]))
train_X, train_Y, X, Y = input('train.csv')
#construct model
pred = model(X, W, b)
# Minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(pred), reduction_indices=1))
# Gradient Descent
learning_rate = 0.1
#optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initializing the variables
init = tf.initialize_all_variables()
get_ipython().magic(u'matplotlib inline')
import collections
import matplotlib.pyplot as plt
training_epochs = 200
batch_size = 300
train_X, train_Y, X, Y = input('train.csv')
acc = []
x = tf.placeholder(tf.float32, [None, 30])
y = tf.placeholder(tf.float32, [None, 16])
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.0
#print(type(y_train[0][0]))
print(type(train_X))
print(type(train_X[0][0]))
print X
_, c = sess.run([optimizer, cost], feed_dict = {x: train_X, y: train_Y})
feef_dict方法无法使用,提示如下:
InvalidArgumentError: 您必须为数据占位符张量 'Placeholder_54' 提供一个值,其数据类型为 float [[Node: Placeholder_54 = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]] 原因来自操作 u'Placeholder_54':
我检查了训练特征数据 X 的数据类型:
train_X type: <type 'numpy.ndarray'>
train_X[0][0]: <type 'numpy.float32'>
train_X size: (300, 30)
place_holder info : Tensor("Placeholder_56:0", shape=(?, 30), dtype=float32)
我不知道为什么它会抱怨。希望有人可以帮忙,谢谢。
tf.reset_default_graph()
。我遇到了一些类似的问题,这是其中帮助解决的方法之一。 - Engineero