IPython Notebook Kernel Crashes when Assessing CNN Accuracy












0















I'm relatively new to TensorFlow. I have built a Logistic Regression classifier and a MultiLayer Perceptron in the past that have worked. Now that I have moved on to the Convolutional Neural Network, I am having some problems with testing accuracy. My code is below. The line I am having trouble with is only the very last line where I am attempting to print the test accuracy figure. The print 1, 2, 3 statements are intended to show this.



### import libraries ###

import tensorflow as tf
import numpy as np
from tqdm import trange

### import mnist data ###

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

##### Begin Computational Graph #####

## initial variable values chosen for ease of use with ReLU ##

# input image vector and reshape to 28x28x1
# 28x28x1 is a single image
# the first dimension will be minibatch size
x = tf.placeholder(
dtype = tf.float32,
shape = [None, 784],
name = "x")

xReshape = tf.reshape(x, [-1, 28, 28, 1])

# placeholder for data labels
y_ = tf.placeholder(
dtype = tf.float32,
shape = [None, 10],
name = "y_")

### First Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b1 = tf.Variable(tf.ones([32]) / 10)

# perform convolution
C1 = tf.nn.conv2d(
input = xReshape,
filter = K1,
strides = [1, 1, 1, 1],
padding = "SAME") + b1

# use activation function
C1_act = tf.nn.relu(C1)

# 2x2 max pool
maxPool1 = tf.nn.max_pool(
value = C1_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### Second Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b2 = tf.Variable(tf.ones([64]) / 10)

# perform convolution
C2 = tf.nn.conv2d(
input = maxPool1,
filter = K2,
strides = [1, 1, 1, 1],
padding = "SAME") + b2

# use activation function
C2_act = tf.nn.relu(C2)

# 2x2 max pool
maxPool2 = tf.nn.max_pool(
value = C2_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### First Fully Connected Layer w/ 256 Hidden Units ###

# flatten maps into one vector
fVect = tf.reshape(maxPool2, [-1, 7 * 7 * 64])

W1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 256],
stddev = 0.01))

fcBias1 = tf.Variable(tf.ones([256]) / 10)

prob_y1 = tf.nn.relu(tf.matmul(fVect, W1) + fcBias1)

### Final Fully Connected layer with 10 hidden Units ###

W2 = tf.Variable(tf.truncated_normal([256, 10],
stddev = 0.01))

fcBias2 = tf.Variable(tf.ones([10]) / 10)

prob_y2 = tf.nn.softmax(logits = (tf.matmul(prob_y1, W2) + fcBias2))

### Loss Function and Optimizer ###

# define loss function
cross_entropy_loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(prob_y2), axis = 1))

# set up gradient descent optimizer

train_step = tf.train.GradientDescentOptimizer(learning_rate = 0.05).minimize(cross_entropy_loss)

##### Train the Network #####

### start the session and initialize global variables ###

# Variable Initializer
init_op = tf.global_variables_initializer()

# Create a Session object, initialize all variables
sess = tf.Session()
sess.run(init_op)

for _ in trange(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {x: batch_xs, y_: batch_ys})

### Test Prediction Accuracy ###

# test trained model
print(1)
correct_prediction = tf.equal(tf.argmax(prob_y2, axis = 1), tf.argmax(y_, axis = 1))
print(2)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(3)
print('Test accuracy: {0}'.format(sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels})))

sess.close()


Apologies for the big code dump. I want make sure the issue is reproducible. The result of this code in my notebook is a pop-up window that says, "The kernel appears to have died. It will restart automatically." I'm hoping this is some small error in my syntax or something, but I've search all the functional documentation and forums and haven't identified my issue.



Any help is appreciated!










share|improve this question























  • This could have something to do with your memory or other resources.When i run this on Google collab(colab.research.google.com) it prints Test accuracy: 0.9466999769210815. You could try there.

    – Mohan Radhakrishnan
    Jan 2 at 8:31













  • @MohanRadhakrishnan Thanks! I will give it a shot.

    – samvoit4
    Jan 2 at 10:59
















0















I'm relatively new to TensorFlow. I have built a Logistic Regression classifier and a MultiLayer Perceptron in the past that have worked. Now that I have moved on to the Convolutional Neural Network, I am having some problems with testing accuracy. My code is below. The line I am having trouble with is only the very last line where I am attempting to print the test accuracy figure. The print 1, 2, 3 statements are intended to show this.



### import libraries ###

import tensorflow as tf
import numpy as np
from tqdm import trange

### import mnist data ###

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

##### Begin Computational Graph #####

## initial variable values chosen for ease of use with ReLU ##

# input image vector and reshape to 28x28x1
# 28x28x1 is a single image
# the first dimension will be minibatch size
x = tf.placeholder(
dtype = tf.float32,
shape = [None, 784],
name = "x")

xReshape = tf.reshape(x, [-1, 28, 28, 1])

# placeholder for data labels
y_ = tf.placeholder(
dtype = tf.float32,
shape = [None, 10],
name = "y_")

### First Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b1 = tf.Variable(tf.ones([32]) / 10)

# perform convolution
C1 = tf.nn.conv2d(
input = xReshape,
filter = K1,
strides = [1, 1, 1, 1],
padding = "SAME") + b1

# use activation function
C1_act = tf.nn.relu(C1)

# 2x2 max pool
maxPool1 = tf.nn.max_pool(
value = C1_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### Second Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b2 = tf.Variable(tf.ones([64]) / 10)

# perform convolution
C2 = tf.nn.conv2d(
input = maxPool1,
filter = K2,
strides = [1, 1, 1, 1],
padding = "SAME") + b2

# use activation function
C2_act = tf.nn.relu(C2)

# 2x2 max pool
maxPool2 = tf.nn.max_pool(
value = C2_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### First Fully Connected Layer w/ 256 Hidden Units ###

# flatten maps into one vector
fVect = tf.reshape(maxPool2, [-1, 7 * 7 * 64])

W1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 256],
stddev = 0.01))

fcBias1 = tf.Variable(tf.ones([256]) / 10)

prob_y1 = tf.nn.relu(tf.matmul(fVect, W1) + fcBias1)

### Final Fully Connected layer with 10 hidden Units ###

W2 = tf.Variable(tf.truncated_normal([256, 10],
stddev = 0.01))

fcBias2 = tf.Variable(tf.ones([10]) / 10)

prob_y2 = tf.nn.softmax(logits = (tf.matmul(prob_y1, W2) + fcBias2))

### Loss Function and Optimizer ###

# define loss function
cross_entropy_loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(prob_y2), axis = 1))

# set up gradient descent optimizer

train_step = tf.train.GradientDescentOptimizer(learning_rate = 0.05).minimize(cross_entropy_loss)

##### Train the Network #####

### start the session and initialize global variables ###

# Variable Initializer
init_op = tf.global_variables_initializer()

# Create a Session object, initialize all variables
sess = tf.Session()
sess.run(init_op)

for _ in trange(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {x: batch_xs, y_: batch_ys})

### Test Prediction Accuracy ###

# test trained model
print(1)
correct_prediction = tf.equal(tf.argmax(prob_y2, axis = 1), tf.argmax(y_, axis = 1))
print(2)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(3)
print('Test accuracy: {0}'.format(sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels})))

sess.close()


Apologies for the big code dump. I want make sure the issue is reproducible. The result of this code in my notebook is a pop-up window that says, "The kernel appears to have died. It will restart automatically." I'm hoping this is some small error in my syntax or something, but I've search all the functional documentation and forums and haven't identified my issue.



Any help is appreciated!










share|improve this question























  • This could have something to do with your memory or other resources.When i run this on Google collab(colab.research.google.com) it prints Test accuracy: 0.9466999769210815. You could try there.

    – Mohan Radhakrishnan
    Jan 2 at 8:31













  • @MohanRadhakrishnan Thanks! I will give it a shot.

    – samvoit4
    Jan 2 at 10:59














0












0








0








I'm relatively new to TensorFlow. I have built a Logistic Regression classifier and a MultiLayer Perceptron in the past that have worked. Now that I have moved on to the Convolutional Neural Network, I am having some problems with testing accuracy. My code is below. The line I am having trouble with is only the very last line where I am attempting to print the test accuracy figure. The print 1, 2, 3 statements are intended to show this.



### import libraries ###

import tensorflow as tf
import numpy as np
from tqdm import trange

### import mnist data ###

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

##### Begin Computational Graph #####

## initial variable values chosen for ease of use with ReLU ##

# input image vector and reshape to 28x28x1
# 28x28x1 is a single image
# the first dimension will be minibatch size
x = tf.placeholder(
dtype = tf.float32,
shape = [None, 784],
name = "x")

xReshape = tf.reshape(x, [-1, 28, 28, 1])

# placeholder for data labels
y_ = tf.placeholder(
dtype = tf.float32,
shape = [None, 10],
name = "y_")

### First Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b1 = tf.Variable(tf.ones([32]) / 10)

# perform convolution
C1 = tf.nn.conv2d(
input = xReshape,
filter = K1,
strides = [1, 1, 1, 1],
padding = "SAME") + b1

# use activation function
C1_act = tf.nn.relu(C1)

# 2x2 max pool
maxPool1 = tf.nn.max_pool(
value = C1_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### Second Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b2 = tf.Variable(tf.ones([64]) / 10)

# perform convolution
C2 = tf.nn.conv2d(
input = maxPool1,
filter = K2,
strides = [1, 1, 1, 1],
padding = "SAME") + b2

# use activation function
C2_act = tf.nn.relu(C2)

# 2x2 max pool
maxPool2 = tf.nn.max_pool(
value = C2_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### First Fully Connected Layer w/ 256 Hidden Units ###

# flatten maps into one vector
fVect = tf.reshape(maxPool2, [-1, 7 * 7 * 64])

W1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 256],
stddev = 0.01))

fcBias1 = tf.Variable(tf.ones([256]) / 10)

prob_y1 = tf.nn.relu(tf.matmul(fVect, W1) + fcBias1)

### Final Fully Connected layer with 10 hidden Units ###

W2 = tf.Variable(tf.truncated_normal([256, 10],
stddev = 0.01))

fcBias2 = tf.Variable(tf.ones([10]) / 10)

prob_y2 = tf.nn.softmax(logits = (tf.matmul(prob_y1, W2) + fcBias2))

### Loss Function and Optimizer ###

# define loss function
cross_entropy_loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(prob_y2), axis = 1))

# set up gradient descent optimizer

train_step = tf.train.GradientDescentOptimizer(learning_rate = 0.05).minimize(cross_entropy_loss)

##### Train the Network #####

### start the session and initialize global variables ###

# Variable Initializer
init_op = tf.global_variables_initializer()

# Create a Session object, initialize all variables
sess = tf.Session()
sess.run(init_op)

for _ in trange(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {x: batch_xs, y_: batch_ys})

### Test Prediction Accuracy ###

# test trained model
print(1)
correct_prediction = tf.equal(tf.argmax(prob_y2, axis = 1), tf.argmax(y_, axis = 1))
print(2)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(3)
print('Test accuracy: {0}'.format(sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels})))

sess.close()


Apologies for the big code dump. I want make sure the issue is reproducible. The result of this code in my notebook is a pop-up window that says, "The kernel appears to have died. It will restart automatically." I'm hoping this is some small error in my syntax or something, but I've search all the functional documentation and forums and haven't identified my issue.



Any help is appreciated!










share|improve this question














I'm relatively new to TensorFlow. I have built a Logistic Regression classifier and a MultiLayer Perceptron in the past that have worked. Now that I have moved on to the Convolutional Neural Network, I am having some problems with testing accuracy. My code is below. The line I am having trouble with is only the very last line where I am attempting to print the test accuracy figure. The print 1, 2, 3 statements are intended to show this.



### import libraries ###

import tensorflow as tf
import numpy as np
from tqdm import trange

### import mnist data ###

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

##### Begin Computational Graph #####

## initial variable values chosen for ease of use with ReLU ##

# input image vector and reshape to 28x28x1
# 28x28x1 is a single image
# the first dimension will be minibatch size
x = tf.placeholder(
dtype = tf.float32,
shape = [None, 784],
name = "x")

xReshape = tf.reshape(x, [-1, 28, 28, 1])

# placeholder for data labels
y_ = tf.placeholder(
dtype = tf.float32,
shape = [None, 10],
name = "y_")

### First Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b1 = tf.Variable(tf.ones([32]) / 10)

# perform convolution
C1 = tf.nn.conv2d(
input = xReshape,
filter = K1,
strides = [1, 1, 1, 1],
padding = "SAME") + b1

# use activation function
C1_act = tf.nn.relu(C1)

# 2x2 max pool
maxPool1 = tf.nn.max_pool(
value = C1_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### Second Convolutional Layer ###

# define kernel for first convolution layer
# initial values are random small numbers
K2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64],
stddev = 0.01))

# define bias for first convolution layer
# initial values of 0.1
b2 = tf.Variable(tf.ones([64]) / 10)

# perform convolution
C2 = tf.nn.conv2d(
input = maxPool1,
filter = K2,
strides = [1, 1, 1, 1],
padding = "SAME") + b2

# use activation function
C2_act = tf.nn.relu(C2)

# 2x2 max pool
maxPool2 = tf.nn.max_pool(
value = C2_act,
ksize = [1,2,2,1],
strides = [1,2,2,1],
padding = "SAME")

### First Fully Connected Layer w/ 256 Hidden Units ###

# flatten maps into one vector
fVect = tf.reshape(maxPool2, [-1, 7 * 7 * 64])

W1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 256],
stddev = 0.01))

fcBias1 = tf.Variable(tf.ones([256]) / 10)

prob_y1 = tf.nn.relu(tf.matmul(fVect, W1) + fcBias1)

### Final Fully Connected layer with 10 hidden Units ###

W2 = tf.Variable(tf.truncated_normal([256, 10],
stddev = 0.01))

fcBias2 = tf.Variable(tf.ones([10]) / 10)

prob_y2 = tf.nn.softmax(logits = (tf.matmul(prob_y1, W2) + fcBias2))

### Loss Function and Optimizer ###

# define loss function
cross_entropy_loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(prob_y2), axis = 1))

# set up gradient descent optimizer

train_step = tf.train.GradientDescentOptimizer(learning_rate = 0.05).minimize(cross_entropy_loss)

##### Train the Network #####

### start the session and initialize global variables ###

# Variable Initializer
init_op = tf.global_variables_initializer()

# Create a Session object, initialize all variables
sess = tf.Session()
sess.run(init_op)

for _ in trange(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {x: batch_xs, y_: batch_ys})

### Test Prediction Accuracy ###

# test trained model
print(1)
correct_prediction = tf.equal(tf.argmax(prob_y2, axis = 1), tf.argmax(y_, axis = 1))
print(2)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(3)
print('Test accuracy: {0}'.format(sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels})))

sess.close()


Apologies for the big code dump. I want make sure the issue is reproducible. The result of this code in my notebook is a pop-up window that says, "The kernel appears to have died. It will restart automatically." I'm hoping this is some small error in my syntax or something, but I've search all the functional documentation and forums and haven't identified my issue.



Any help is appreciated!







python-3.x tensorflow jupyter-notebook






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 20:43









samvoit4samvoit4

134




134













  • This could have something to do with your memory or other resources.When i run this on Google collab(colab.research.google.com) it prints Test accuracy: 0.9466999769210815. You could try there.

    – Mohan Radhakrishnan
    Jan 2 at 8:31













  • @MohanRadhakrishnan Thanks! I will give it a shot.

    – samvoit4
    Jan 2 at 10:59



















  • This could have something to do with your memory or other resources.When i run this on Google collab(colab.research.google.com) it prints Test accuracy: 0.9466999769210815. You could try there.

    – Mohan Radhakrishnan
    Jan 2 at 8:31













  • @MohanRadhakrishnan Thanks! I will give it a shot.

    – samvoit4
    Jan 2 at 10:59

















This could have something to do with your memory or other resources.When i run this on Google collab(colab.research.google.com) it prints Test accuracy: 0.9466999769210815. You could try there.

– Mohan Radhakrishnan
Jan 2 at 8:31







This could have something to do with your memory or other resources.When i run this on Google collab(colab.research.google.com) it prints Test accuracy: 0.9466999769210815. You could try there.

– Mohan Radhakrishnan
Jan 2 at 8:31















@MohanRadhakrishnan Thanks! I will give it a shot.

– samvoit4
Jan 2 at 10:59





@MohanRadhakrishnan Thanks! I will give it a shot.

– samvoit4
Jan 2 at 10:59












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998796%2fipython-notebook-kernel-crashes-when-assessing-cnn-accuracy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998796%2fipython-notebook-kernel-crashes-when-assessing-cnn-accuracy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$