在卷积层中调用模型文件
from mnist import model
data = read_data_sets('MNIST_data', one_hot=True)
with tf.variable_scope('convolutional'):
x = tf.placeholder(tf.float32, [None, 784])
keep_prob = tf.placeholder(tf.float32)
y, variables = model.convolutional(x,keep_prob)
_y = tf.placeholder(tf.float32, [None, 10])
cross_entropy = -tf.reduce_sum(_y * tf.log(y))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(_y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver=tf.train.Saver(variables)
with tf.Session() as sess:
merged_summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter('/tmp/mnist_log/1', sess.graph)
summary_writer.add_graph(sess.graph)
1.定义完卷积的模型之后,定义卷积的文件:model->convolutional
import os
from mnist import model
import tensorflow as tf
from mnist import input_data
data = input_data.read_data_sets('MNIST_data', one_hot=True)
# model
with tf.variable_scope('convolutional'):
x = tf.placeholder(tf.float32, [None, 784], name='x')
keep_prob = tf.placeholder(tf.float32)
y, variables = model.convolutional(x, keep_prob)
# train
y_ = tf.placeholder(tf.float32, [None, 10], name='y')
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = -tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # 随机梯度下降的方式
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # 判断参数是否相等
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver(variables)
with tf.Session() as sess:
merged_summary_op = tf.summary.merge_all() # 合并参数,操作符
summary_writer = tf.summary.FileWriter('/tmp/mnist_log/1', sess.graph) # 将参数的路径、输入输出图放到哪里
summary_writer.add_graph(sess.graph) # 把图加进来
sess.run(tf.global_variables_initializer())
Adam0pitimizer 随机梯度下降