执行简单的tensforflow片段原因:内核似乎已经死亡。它会自动重启

时间:2016-11-23 09:24:37

标签: tensorflow ubuntu-14.04 ipython-notebook

我正在尝试创建简单的网络并使用两个转换层和2个fc层在mnist上训练它 这是达到sess.run()崩溃时的代码。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from tensorflow.examples.tutorials.mnist import input_data
#read data
data = input_data.read_data_sets('data/MNIST/', one_hot=True)

#useful info
image_size =28
img_size_flattened = 728
num_class = 10

#create placeholders 
x_data = tf.placeholder(tf.float32,[None,img_size_flattened])

#since our conv-layer accepts images in format of [#,img_height,img_widths,channels] we need to have a reshaped version as well
x_data_reshaped = tf.reshape(x_data,[-1,image_size,image_size,1])
y_label_vec = tf.placeholder(tf.float32,[None,num_class])
y_label_true_indx = tf.arg_max(y_label_vec,dimension=1)

#create helper methods for conv layers creations
#create methods for creating weights and biases 
def add_weight (shape):
    return tf.Variable(tf.truncated_normal(shape,stddev=0.05))
def add_bias(length):
    return tf.Variable(tf.ones(length))

#returns layer and weights
def add_conv(input, num_channel, kernel_size, num_kernel, use_pool=True):

    shape = [kernel_size,kernel_size,num_channel,num_kernel]
    weights = add_weight(shape)
    biases = add_bias(num_kernel)
    layer = tf.nn.conv2d(input=input, filter=weights, strides=[1,1,1,1],padding='SAME')
    if use_pool:
        layer = tf.nn.max_pool(layer, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

    layer = tf.nn.relu(layer)    
    return layer, weights

#create fc layer, but since we need flattened vectors  to connect to fc, lets create a flatten helper function
#returns layerflattended, and numberoffeatures(volumn length)
def flatten(layer):
    layer_shape = layer.get_shape()
    numberof_features = layer_shape[1:4].num_elements()
    layer_reshaped = tf.reshape(layer, [-1,numberof_features])
    return layer_reshaped,numberof_features

#now create helper function for fc 
def add_fc(flattenedlayer,num_input,num_output,Activation=None):
    weights = add_weight([num_input,num_output])
    biases = add_bias(num_output)
    output = tf.matmul(flattenedlayer,weights) + biases
    if Activation != None:
        output = Activation(output)
    return output    

#now lets create layers 
laye_conv1,w1 = add_conv(input=x_data_reshaped, num_channel=1, kernel_size=5, num_kernel=16)
layer_conv2,w2 = add_conv(input=laye_conv1, num_channel=16, kernel_size=5, num_kernel=36)
flat_Layer,num_features = flatten(layer_conv2)
layer_fc1 = add_fc(flattenedlayer=flat_Layer,num_input=num_features,num_output=128, Activation=tf.nn.relu)
layer_fc2 = add_fc(layer_fc1,num_input=128,num_output=10)

#calculate prediction 
pred = tf.nn.softmax(layer_fc2)
pred_labl_true_index = tf.argmax(pred, dimension=1)

#calculate accuracy
correctPredcition = tf.equal(pred_labl_true_index, y_label_true_indx)
accuracy = tf.reduce_mean(tf.cast(correctPredcition,tf.float32))

#crossendtropy
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(layer_fc2,y_label_vec)
loss = tf.reduce_mean(cross_entropy)
train = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

data.test.true_lbl_indexes = [np.argmax(l) for l in data.test.labels]
for i in range(1000):
    x_dt, x_lbl = data.train.next_batch(100)
    dict = {x_data:x_dt, y_label_vec:x_lbl}
    sess.run(train, feed_dict=dict)
    if i%20 == 0:
        test_dic = {x_data:data.test.images,
                    y_label_vec:data.test.labels,
                    y_label_true_indx: data.test.true_lbl_indexes}
        acc = sess.run(accuracy,feed_dict=test_dic)
        print (acc)

我查看了服务器控制台,看看那里是否有任何错误消息,但遗憾的是什么都没有!我不知道是什么导致了这次崩溃!

更新
重新启动操作系统并再次重新运行脚本后,它现在会出现此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-a3d8382e9475> in <module>()
     80     x_dt, x_lbl = data.train.next_batch(100)
     81     dict = {x_data:x_dt, y_label_vec:x_lbl}
---> 82     sess.run(train, feed_dict=dict)
     83     if i%20 == 0:
     84         test_dic = {x_data:data.test.images,

/media/seyyedhossein/tmpstore/tensorflow_virtenv_p3/lib/python3.4/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    708     try:
    709       result = self._run(None, fetches, feed_dict, options_ptr,
--> 710                          run_metadata_ptr)
    711       if run_metadata:
    712         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/media/seyyedhossein/tmpstore/tensorflow_virtenv_p3/lib/python3.4/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    885                 'Cannot feed value of shape %r for Tensor %r, '
    886                 'which has shape %r'
--> 887                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    888           if not self.graph.is_feedable(subfeed_t):
    889             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (100, 784) for Tensor 'x_data:0', which has shape '(?, 728)'

1 个答案:

答案 0 :(得分:1)

  

ValueError:无法为Tensor提供形状值(100,784)   &#39; x_data:0&#39;,有形状&#39;(?,728)&#39;

这意味着当它需要一个形状的张量(?,728)时,你正在为网络提供一些形状(?,784)。

我认为你错误地计算了扁平形状。 28 * 28 = 784.我会改变输入占位符的大小。