Answer the question
In order to leave comments, you need to log in
Bug in tensorflow that doesn't exist or how to fix it?
import tensorflow as tf
import numpy as np
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = np.reshape(x_train/255.0, [-1, 28 * 28]) , np.reshape(x_test/255.0, [-1, 28 * 28])
y_train = tf.keras.utils.to_categorical(y_train, 10)
def next_batch(x_train, y_train, batch_size):
shuffled_index = np.random.randint(0, len(y_train), batch_size)
x_batch, y_batch = x_train[shuffled_index], y_train[shuffled_index]
return x_batch, y_batch
tensor = tf.placeholder(tf.float32, [None, 28 * 28], name = "X")
target = tf.placeholder(tf.int32, [None, 10], name = "y")
W = tf.Variable(tf.random_normal(shape = (28 * 28, 10), dtype= tf.float32))
b = tf.Variable(tf.random_normal(shape = (10,), dtype= tf.float32))
output = tf.add(tf.matmul(tensor, W), b)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits= output, labels= target))
optimizer = tf.train.AdamOptimizer(learning_rate = 0.01)
train_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
epoches = 10000
with tf.Session() as sess:
init.run()
for i in range(epoches):
loss = 0
x_batch, y_batch = next_batch(x_train, y_train, 24)
_, loss = sess.run([train_op, loss], feed_dict= {tensor: x_batch, target: y_batch})
if (i%1000 + 1) == 0:
print(loss)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question