2016-11-23 152 views
0

我正在嘗試創建簡單的網絡,並在mnist上用兩個conv和2 fc圖層進行訓練。
這是在達到sess.run()崩潰時的代碼。執行簡單的tensforflow片段原因:內核似乎已經死亡。它會自動重啓

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

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

這意味着你是餵養網絡的東西的形狀( 784),當它期望一個形狀張量(?,728)。

我認爲你計算了扁平化的形狀是錯誤的。 28 * 28 = 784.我會改變輸入佔位符的大小。

+0

謝謝男人:)我不相信這一點!我查看了這些數字,並將它們都讀爲784!上帝讓我困惑了最後4個小時! – Breeze