6,721
社区成员
发帖
与我相关
我的任务
分享
import tensorflow as tf
import numpy as np
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer(['test_train.tfrecords'])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
}
)
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [28, 28, 3])
img = tf.cast(img, tf.float32) * (1./ 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return img, label
img, label = read_and_decode("test_train.tfrecords")
img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=100, capacity=2000,
min_after_dequeue=1000)
x = tf.placeholder("float",[None,784])
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,w)+b)
y_ = tf.placeholder("float",[None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=100, capacity=2000,
min_after_dequeue=1000)
threads = tf.train.start_queue_runners(sess=sess)
for i in range(1000):
img_xs,label_xs = sess.run([img_batch,label_batch])
sess.run(train_step, feed_dict={x: img_xs,y_:label_xs})
import tensorflow as tf
import numpy as np
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer(['test_train.tfrecords'])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
}
)
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [28*28*3])
img = tf.cast(img, tf.float32) * (1./ 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return img, label
img, label = read_and_decode("test_train.tfrecords")
img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=100, capacity=2000,
min_after_dequeue=1000)
x = tf.placeholder("float",[None,28*28*3])
w = tf.Variable(tf.zeros([28*28*3,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,w)+b)
y_ = tf.placeholder("float",[None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=100, capacity=2000,
min_after_dequeue=1000)
threads = tf.train.start_queue_runners(sess=sess)
for i in range(1000):
img_xs,label_xs = sess.run([img_batch,label_batch])
sess.run(train_step, feed_dict={x: img_xs,y_:label_xs})