2016-05-28 11 views
1

私はテンソルフロー0.8.0をPython v2.7で使用しています。私のIDEはPyCharmで、私のOSがLinuxのUbuntuの私は、次のコードは、私のコンピュータがフリーズする原因となる、および/またはクラッシュすることを気づい14.04sess.run()を使用するとTensorflowがクラッシュする

です:

# you will need these files! 
# https://www.kaggle.com/c/digit-recognizer/download/train.csv 
# https://www.kaggle.com/c/digit-recognizer/download/test.csv 

import numpy as np 
import pandas as pd 
import tensorflow as tf 
import matplotlib.pyplot as plt 
import matplotlib.cm as cm 

# read in the image data from the csv file 
# the format is: imagelabel pixel0 pixel1 ... pixel783 (there are 42,000 rows like this) 
data = pd.read_csv('../train.csv') 
labels = data.iloc[:,:1].values.ravel() # shape = (42000, 1) 
labels_count = np.unique(labels).shape[0] # = 10 
images = data.iloc[:,1:].values # shape = (42000, 784) 
images = images.astype(np.float64) 
image_size = images.shape[1] 
image_width = image_height = np.sqrt(image_size).astype(np.int32) # since these images are sqaure... hieght = width 


# turn all the gray-pixel image-values into percentages of 255 
# a 1.0 means a pixel is 100% black, and 0.0 would be a pixel that is 0% black (or white) 
images = np.multiply(images, 1.0/255) 


# create oneHot vectors from the label #s 
oneHots = tf.one_hot(labels, labels_count, 1, 0) #shape = (42000, 10) 


#split up the training data even more (into validation and train subsets) 
VALIDATION_SIZE = 3167 

validationImages = images[:VALIDATION_SIZE] 
validationLabels = labels[:VALIDATION_SIZE] 

trainImages = images[VALIDATION_SIZE:] 
trainLabels = labels[VALIDATION_SIZE:] 






# ------------- Building the NN ----------------- 

# set up our weights (or kernals?) and biases for each pixel 
def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=.1) 
    return tf.Variable(initial) 

def bias_variable(shape): 
    initial = tf.constant(.1, shape=shape, dtype=tf.float32) 
    return tf.Variable(initial) 


# convolution 
def conv2d(x, W): 
    return tf.nn.conv2d(x, W, [1,1,1,1], 'SAME') 

# pooling 
def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 


# placeholder variables 
# images 
x = tf.placeholder('float', shape=[None, image_size]) 
# labels 
y_ = tf.placeholder('float', shape=[None, labels_count]) 



# first convolutional layer 
W_conv1 = weight_variable([5, 5, 1, 32]) 
b_conv1 = bias_variable([32]) 

# turn shape(40000,784) into (40000,28,28,1) 
image = tf.reshape(trainImages, [-1,image_width , image_height,1]) 
image = tf.cast(image, tf.float32) 
# print (image.get_shape()) # =>(40000,28,28,1) 




h_conv1 = tf.nn.relu(conv2d(image, W_conv1) + b_conv1) 
# print (h_conv1.get_shape()) # => (40000, 28, 28, 32) 
h_pool1 = max_pool_2x2(h_conv1) 
# print (h_pool1.get_shape()) # => (40000, 14, 14, 32) 





# second convolutional layer 
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) 
#print (h_conv2.get_shape()) # => (40000, 14,14, 64) 
h_pool2 = max_pool_2x2(h_conv2) 
#print (h_pool2.get_shape()) # => (40000, 7, 7, 64) 




# densely connected layer 
W_fc1 = weight_variable([7 * 7 * 64, 1024]) 
b_fc1 = bias_variable([1024]) 

# (40000, 7, 7, 64) => (40000, 3136) 
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) 
#print (h_fc1.get_shape()) # => (40000, 1024) 





# dropout 
keep_prob = tf.placeholder('float') 
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 
print h_fc1_drop.get_shape() 


#readout layer for deep neural net 
W_fc2 = weight_variable([1024,labels_count]) 
b_fc2 = bias_variable([labels_count]) 
print b_fc2.get_shape() 
mull= tf.matmul(h_fc1_drop, W_fc2) 
print mull.get_shape() 
print 
mull2 = mull + b_fc2 
print mull2.get_shape() 

y = tf.nn.softmax(mull2) 



# dropout 
keep_prob = tf.placeholder('float') 
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 


sess = tf.Session() 
sess.run(tf.initialize_all_variables()) 

print sess.run(mull[0,2]) 

LASE行はクラッシュが発生します。

print sess.run(mull [0,2])

これは、基本的に非常に大きな2次元アレイの1つの場所です。 sess.runの何かが原因です。私はまた、スクリプトの問題のポップアップを取得しています... Googleスクリプトの何らかの並べ替え(多分それはテンソルフローですか?)。コンピュータが完全にフリーズしているため、リンクをコピーできません。

答えて

1

私はその小さな見かけの大きさにもかかわらずmull[0, 2] —が—は、複数の畳み込み、MAX-プーリング、および大規模な行列の乗算を含む、非常に大規模な計算に依存しているため、問題が発生した疑いがあります。そのため、コンピュータが長期間完全にロードされたり、メモリが不足したりすることがあります。 (topを実行して、どのリソースがTensorFlowを実行しているpythonプロセスによって使用されているかを確認する必要があります。)

TensorFlowグラフは、 40000枚の画像が含まれている全体のトレーニングデータセット、trainImages、:

image = tf.reshape(trainImages, [-1,image_width , image_height,1]) 
image = tf.cast(image, tf.float32) 

代わりに、あなたが個々の訓練例を養う、またはのミニバッチできるためにどのtf.placeholder()の観点でネットワークを定義するために、より効率的です例。詳細については、documentation on feedingを参照してください。特に、0行目のmullにのみ関心があるので、0番目の例をtrainImagesから入力し、計算を実行して必要な値を生成する必要があります。 (現在のプログラムでは、他のすべての例の結果も計算され、最後のスライス演算子で破棄されます)。

+0

ありがとう!理にかなっている – rikkitikkitumbo

0

セッションをデフォルトとして設定し、セッションを実行する前に変数を初期化すると問題が解決する場合があります。

import tensorflow as tf 

sess = tf.Session() 
g = tf.ones([25088]) 

sess.as_default(): 
    tf.initialize_all_variables().run() 
    results = sess.run(g) 

    print results 
+0

hmm、それでも同じ問題が発生しました。私は – rikkitikkitumbo

関連する問題