我一直在努力理解传递给张量流的数据。 我想用张量流进行分类。我有一个数据帧,有5个功能(列)我的X和89行(数据点)。我有一个目标变量' y'在第6列中有5个班级。
整个数据帧的形状为89 X 6.
此外,我一直试图实施的代码。
import tensorflow as tf
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X1, y1, test_size=0.3, random_state=45)
#making target variable column in dummy
i = ['tumor'] #6th column name is 'tumor'
y_train1 = pd.get_dummies(y_train, columns = i, drop_first = False)
y_test1 = pd.get_dummies(y_test, columns = i, drop_first = False)
# I am passing target variable as dataframe of dummy variables of my classes. is it correct? Should I split Y variable into dummy variables?
n_nodes_hl1 = 50
n_nodes_hl2 = 50
n_nodes_hl3 = 50
n_classes = 5
batch_size = 10
x = tf.placeholder('float', [None, len(X_train)]) #height X width, part where I am struggling.
y = tf.placeholder('float')
def neural_network_model(data):
#matching the placeholder's dimension len(X_train) for layer 1
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([len(X_train), n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes]))}
#input data * weights + biases
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))
optimizer = tf.train.AdamOptimizer().minimize(cost) #default learning rate = 0.001
hm_epochs = 5
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
epoch_loss = 0
i=0
while i <len(X_train):
start = i
end = i + batch_size
batch_x = np.array(X_train[start:end])
batch_y = np.array(y_train1[start:end])
_,c = sess.run([optimizer, cost], feed_dict = {x:batch_x , y:batch_y})
epoch_loss += c
i += batch_size
print ('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)
correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print ('accuracy:', accuracy.eval({x:X_test, y:y_test1}))
train_neural_network(X_train)
如果X_train形状为62X5,则错误为
Argument must be a dense tensor.
[62 rows x 5 columns] - got shape [62, 5], but wanted [].
有人可以解释一下将数据传递给张量流或占位符和维度吗?谢谢。