我有两个numpy genfromtxt
文件:第一个名为data_pixels
的文件包含我的训练样例,每行为3072维,第二个名为classes_dataset
,其中包含我的数据标签。
以下代码l使用此link的基础。
我的问题是Launch the graph part (see below)
batch_x, batch_y = mnist.train.next_batch(batch_size)
如何使我的两个向量(classes_dataset和data_pixels)适应这行代码以生成张量流批次?
这是我的代码:
import tensorflow as tf
import numpy as np
data_pixels=np.genfromtxt("pixels_dataset.csv", delimiter=',') #training data
data_pixels
array([[ 1., 1., 1., ..., 1., 1., 1.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 1., 1., 1., ..., 0., 0., 0.],
[ 1., 1., 1., ..., 1., 1., 1.],
[ 0., 0., 0., ..., 0., 0., 0.]])
data_pixels.shape
(2440, 3072)
班级
classes_dataset=np.genfromtxt("labels.csv",dtype=np.str , delimiter='\t')
classes_dataset
array(['m', 'm', 'i', ..., '3', '9', '9'],
dtype='|S5')
课程数量为:
a=len(c.Counter(set(classes_dataset)))
66
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 256 # 2nd layer number of features
n_input= data_pixels.shape[1]
n_classes = len(c.Counter(set(classes_dataset)))
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Initializing the variables
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
#total_batch = int(mnist.train.num_examples/batch_size)
total_batch = data_pixels.shape[0]/batch_size
# Loop over all batches
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size) # How to put my dataset_pixels and classes_dataset into this format
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print "Epoch:", '%04d' % (epoch+1), "cost=", \
"{:.9f}".format(avg_cost)
print "Optimization Finished!"
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})