学海拾贝,偶有所得,虽未敢言精深,然点滴所得亦心血所铸,如需引用请注明出处.
如下代码可以建立2个不同结构的模型,在两个计算图中同时训练.
要点在代码后面:
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tfdef conv2d(x, W):
"""conv2d returns a 2d convolution layer with full stride."""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
"""max_pool_2x2 downsamples a feature map by 2X."""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
"""weight_variable generates a weight variable of a given shape."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""bias_variable generates a bias variable of a given shape."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def model_str1(x):#模型机构1
with tf.name_scope('reshape'):
x_image = tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
with tf.name_scope('pool1'):
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
with tf.name_scope('pool2'):
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1'):
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
with tf.name_scope('dropout'):
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
with tf.name_scope('fc2'):
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
return y_conv, keep_prob
def model_str2(x):#模型机构2
with tf.name_scope('reshape'):
x_image = tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
with tf.name_scope('pool1'):
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
with tf.name_scope('pool2'):
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1'):
W_fc1 = weight_variable([7 * 7 * 64, 512])
b_fc1 = bias_variable([512])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
with tf.name_scope('fc2'):
W_fc2 = weight_variable([512, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1, W_fc2) + b_fc2
return y_conv
def main():
# Import data
mnist = input_data.read_data_sets("F:/pokertraining/mnist/mnist", one_hot=True)
#以两种不同的结构建立两个模型
G1=tf.Graph()
Sess1=tf.Session(graph=G1)
with Sess1.as_default():
with G1.as_default():
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
y_conv, keep_prob = model_str1(x)
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y_conv)
cross_entropy = tf.reduce_mean(cross_entropy)
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)
Sess1.run(tf.global_variables_initializer())
G2=tf.Graph()
Sess2=tf.Session(graph=G2)
with Sess2.as_default():
with G2.as_default():
x2 = tf.placeholder(tf.float32, [None, 784])
y_2 = tf.placeholder(tf.float32, [None, 10])
y_conv2=model_str2(x2)
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_2,logits=y_conv2)
cross_entropy = tf.reduce_mean(cross_entropy)
with tf.name_scope('adam_optimizer'):
train_step2 = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv2, 1), tf.argmax(y_2, 1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy2 = tf.reduce_mean(correct_prediction)
Sess2.run(tf.global_variables_initializer())#初始化权重
#训练模型
for i in range(5000):
batch = mnist.train.next_batch(50)
if (i+1)%100==0:
train_accuracy = Sess1.run(accuracy,feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print("training batches=",i,"model accuracy=",train_accuracy)
train_accuracy2 = Sess2.run(accuracy2,feed_dict={x2: batch[0], y_2: batch[1]})
print("training batches=",i,"model2 accuracy=",train_accuracy2)
Sess1.run(train_step,feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
Sess2.run(train_step2,feed_dict={x2: batch[0], y_2: batch[1]})
if __name__ == '__main__':
main()
要点:
1.关键词
with Sess2.as_default():
with G2.as_default():...
在构成模型,模型初始化,或者加载模型的时候一定要这两句.
2.计算图变量的作用域:
凡是不被外部使用的变量作用域为本计算图内,所以在两计算图中可以用同样的名字:例如:cross_entropy,correct_prediction
凡是需要被外部引用的变量,比如需要用feed_dic填入数据的,或者用sess.run的张量,使用同样变量名就会出问题,为此,需要用不同变量名,如:x,x2,y_,y_2, y_conv, y_conv2,train_step,train_step2.
如果在上述代码中加入Sess1.run(correct_prediction,feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
Sess2.run(correct_prediction,feed_dict={x2: batch[0], y_2: batch[1]})
就会报出变量不在图内的错误.因为此时sess run的correct_prediction的作用域出了问题.(或许这里应该称之为张量)
3.不能够用xxx.eval(...)来代替Sess.run(xxx,....),因为xxx.eval(...)使用的是默认的计算图.