tensorflow neural network, TypeError: eval() got multiple values for argument 'feed_dict'
tensorflow neural network, TypeError: eval() got multiple values for argument 'feed_dict'
我正在使用tensorflow实现一个4隐藏层sigmoid激活神经网络,下面是代码。
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
import pandas as pd
#importing and preprocessing data
def get_data():
df = pd.read_csv('date_train.csv')
date_train = df.as_matrix()
X1 = date_train[:, :-1]
Y_float = date_train[:, -1]
Y = Y_float.astype(int)
#standardization
X = preprocessing.scale(X1)
N, D = X.shape
K = len(set(Y)) #length of possible y values
df_test = pd.read_csv('date_test.csv')
X_test = df_test.as_matrix()
df_ans = pd.read_csv('date_test_ans.csv')
Y_ans = df_ans.as_matrix()
Y_test = Y_ans[:,-1]
return X, Y, N, D, K, X_test, Y_test
def init_weights(shape):
return tf.Variable(tf.random_normal(shape,stddev=0.01))
#model
def forward(tfX, W1, b1, W2, b2, W3, b3, W4, b4, W5, b5):
l1 = tf.add(tf.matmul(tfX, W1), b1)
l1 = tf.nn.sigmoid(l1)
l2 = tf.add(tf.matmul(l1, W2), b2)
l2 = tf.nn.sigmoid(l2)
l3 = tf.add(tf.matmul(l2, W3), b3)
l3 = tf.nn.sigmoid(l3)
l4 = tf.add(tf.matmul(l3, W4), b4)
l4 = tf.nn.sigmoid(l4)
output = tf.matmul(l4, W5) + b5
return output
X, Y, N, D, K, X_test, Y_test = get_data()
T = np.zeros((N,K))
for i in range(N):
T[i,Y[i]] = 1
# params
h1 = 18
h2 = 18
h3 = 18
h4 = 18
learning_rate = 0.01
epochs = 100
tfX = tf.placeholder(tf.float32, [None, D])
tfY = tf.placeholder(tf.float32, [None, K])
W1 = init_weights([D,h1])
b1 = init_weights([h1])
W2 = init_weights([h1,h2])
b2 = init_weights([h2])
W3 = init_weights([h2,h3])
b3 = init_weights([h3])
W4 = init_weights([h3,h4])
b4 = init_weights([h4])
W5 = init_weights([h4,K])
b5 = init_weights([K])
#train
logits = forward(tfX, W1, b1, W2, b2, W3, b3, W4, b4, W5, b5)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tfY, logits=logits))
train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)
predict_op = tf.argmax(logits, 1)
correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(tfY,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(epochs):
sess.run(train_op, feed_dict={tfX: X, tfY: T})
pred = sess.run(predict_op, feed_dict={tfX: X, tfY: T})
if i % 100 == 0:
print("Accuracy Rate", np.mean(Y == pred))
print(accuracy.eval(predict_op, feed_dict={tfX: X_test, tfY: Y_test}))
代码训练部分 运行 很好,但是当我尝试评估模型时它产生了这个错误(注意这是我第一次使用 eval outisde 教程):
TypeError: eval() got multiple values for argument 'feed_dict'
另外 运行 如果重要的话,这在 tensorflow-gpu 上
由于这不是交互式会话,您应该在会话中使用 eval。
print(accuracy.eval(predict_op, feed_dict={tfX: X_test, tfY: Y_test},session = sess))
或者
with tf.Session() as sess:
print(accuracy.eval(predict_op, feed_dict={tfX: X_test, tfY: Y_test}))
或者,将 sess 声明为交互式会话,如下所示:
sess = tf.InteractiveSession()
并像您在当前代码中使用的那样使用 eval。
我正在使用tensorflow实现一个4隐藏层sigmoid激活神经网络,下面是代码。
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
import pandas as pd
#importing and preprocessing data
def get_data():
df = pd.read_csv('date_train.csv')
date_train = df.as_matrix()
X1 = date_train[:, :-1]
Y_float = date_train[:, -1]
Y = Y_float.astype(int)
#standardization
X = preprocessing.scale(X1)
N, D = X.shape
K = len(set(Y)) #length of possible y values
df_test = pd.read_csv('date_test.csv')
X_test = df_test.as_matrix()
df_ans = pd.read_csv('date_test_ans.csv')
Y_ans = df_ans.as_matrix()
Y_test = Y_ans[:,-1]
return X, Y, N, D, K, X_test, Y_test
def init_weights(shape):
return tf.Variable(tf.random_normal(shape,stddev=0.01))
#model
def forward(tfX, W1, b1, W2, b2, W3, b3, W4, b4, W5, b5):
l1 = tf.add(tf.matmul(tfX, W1), b1)
l1 = tf.nn.sigmoid(l1)
l2 = tf.add(tf.matmul(l1, W2), b2)
l2 = tf.nn.sigmoid(l2)
l3 = tf.add(tf.matmul(l2, W3), b3)
l3 = tf.nn.sigmoid(l3)
l4 = tf.add(tf.matmul(l3, W4), b4)
l4 = tf.nn.sigmoid(l4)
output = tf.matmul(l4, W5) + b5
return output
X, Y, N, D, K, X_test, Y_test = get_data()
T = np.zeros((N,K))
for i in range(N):
T[i,Y[i]] = 1
# params
h1 = 18
h2 = 18
h3 = 18
h4 = 18
learning_rate = 0.01
epochs = 100
tfX = tf.placeholder(tf.float32, [None, D])
tfY = tf.placeholder(tf.float32, [None, K])
W1 = init_weights([D,h1])
b1 = init_weights([h1])
W2 = init_weights([h1,h2])
b2 = init_weights([h2])
W3 = init_weights([h2,h3])
b3 = init_weights([h3])
W4 = init_weights([h3,h4])
b4 = init_weights([h4])
W5 = init_weights([h4,K])
b5 = init_weights([K])
#train
logits = forward(tfX, W1, b1, W2, b2, W3, b3, W4, b4, W5, b5)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tfY, logits=logits))
train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)
predict_op = tf.argmax(logits, 1)
correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(tfY,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(epochs):
sess.run(train_op, feed_dict={tfX: X, tfY: T})
pred = sess.run(predict_op, feed_dict={tfX: X, tfY: T})
if i % 100 == 0:
print("Accuracy Rate", np.mean(Y == pred))
print(accuracy.eval(predict_op, feed_dict={tfX: X_test, tfY: Y_test}))
代码训练部分 运行 很好,但是当我尝试评估模型时它产生了这个错误(注意这是我第一次使用 eval outisde 教程):
TypeError: eval() got multiple values for argument 'feed_dict'
另外 运行 如果重要的话,这在 tensorflow-gpu 上
由于这不是交互式会话,您应该在会话中使用 eval。
print(accuracy.eval(predict_op, feed_dict={tfX: X_test, tfY: Y_test},session = sess))
或者
with tf.Session() as sess:
print(accuracy.eval(predict_op, feed_dict={tfX: X_test, tfY: Y_test}))
或者,将 sess 声明为交互式会话,如下所示:
sess = tf.InteractiveSession()
并像您在当前代码中使用的那样使用 eval。