2017-01-29 71 views
0

我是tensorflow的新人,並一直在試驗苗條的實驗。我試圖將tensorflow教程中的MNIST教程翻譯成簡潔的語法。它一直在處理與模型中的一組未經處理的圖像。然後我在代碼中添加了一個tf.train_batch線程,當我運行整個文件時,它停止工作。給錯誤Tensorflow苗條和斷言錯誤

Traceback (most recent call last): 
    File ".../slim.py", line 43, in <module> 
    train_op = slim.learning.create_train_op(loss, optimiser) 
    File "...\Python\Python35\lib\site-packages\tensorflow\contrib\slim\python\slim\learning.py", line 442, in create_train_op 
    assert variables_to_train 
AssertionError 

不過,我可以有選擇地重新運行create_train_op行,然後訓練模型,雖然損失的功能在這裏不減少,基本上這是行不通的。這仍然使我可以從張量板(下面附上)獲得圖形可視化,並且我看不到任何錯誤。

我知道我做錯了什麼,但不知道它在哪裏。

import tensorflow as tf 
import time 
from tensorflow.examples.tutorials.mnist import input_data 
import tensorflow.contrib.slim as slim 


def model(inputs, is_training=True): 
    end_points = {} 
    with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, 
         weights_initializer=tf.truncated_normal_initializer(stddev=0.1)): 
     net = slim.conv2d(inputs, 32, [5, 5], scope="conv1") 
     end_points['conv1'] = net 
     net = slim.max_pool2d(net, [2, 2], scope="pool1") 
     end_points['pool1'] = net 
     net = slim.conv2d(net, 64, [5, 5], scope="conv2") 
     end_points['conv2'] = net 
     net = slim.max_pool2d(net, [2, 2], scope="pool2") 
     end_points['pool2'] = net 
     net = slim.flatten(net, scope="flatten") 
     net = slim.fully_connected(net, 1024, scope="fc1") 
     end_points['fc1'] = net 
     net = slim.dropout(net, keep_prob=0.75, is_training=is_training, scope="dropout") 
     net = slim.fully_connected(net, 10, scope="final", activation_fn= None) 
     end_points['final'] = net 
    return net, end_points 

mnist = input_data.read_data_sets("MNIST_data/", one_hot=False) 
batch = mnist.train.next_batch(20000) 

x_image = tf.reshape(batch[0], [-1,28,28,1]) 
label = tf.one_hot(batch[1], 10) 

image, labels = tf.train.batch([x_image[0], label[0]], batch_size= 100) 

with tf.Graph().as_default(): 
    tf.logging.set_verbosity(tf.logging.DEBUG) 
    logits, _ = model(image) 
    predictions = tf.nn.softmax(logits) 
    loss = slim.losses.softmax_cross_entropy(predictions, labels) 
    config = tf.ConfigProto() 
    optimiser = tf.train.AdamOptimizer(1e-4) 
    train_op = slim.learning.create_train_op(loss, optimiser) 
    thisloss = slim.learning.train(train_op, "C:/temp/test2", number_of_steps=100, save_summaries_secs=30, session_config=config) 

enter image description here

回答

0

你需要在同一張圖下創建的所有OPS,包括輸入數據

with tf.Graph().as_default(): 
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=False) 
    batch = mnist.train.next_batch(20000) 

    x_image = tf.reshape(batch[0], [-1,28,28,1]) 
    label = tf.one_hot(batch[1], 10) 

    image, labels = tf.train.batch([x_image[0], label[0]], batch_size= 100) 

    tf.logging.set_verbosity(tf.logging.DEBUG) 
    logits, _ = model(image) 
    predictions = tf.nn.softmax(logits) 
    loss = slim.losses.softmax_cross_entropy(predictions, labels) 
    config = tf.ConfigProto() 
    optimiser = tf.train.AdamOptimizer(1e-4) 
    train_op = slim.learning.create_train_op(loss, optimiser) 
    thisloss = slim.learning.train(train_op, "C:/temp/test2", number_of_steps=100, save_summaries_secs=30, session_config=config)