TensorFlow Dense和ReLU层导致分段错误错误

时间:2017-06-16 13:17:55

标签: python tensorflow segmentation-fault

我正在使用TensorFlow创建一个神经网络。 我在help.py文件中有一些帮助函数:

import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

def read_mnist(folder_path="MNIST_data/"):
    return input_data.read_data_sets(folder_path, one_hot=True)

def build_training(y_labels, y_output, learning_rate=0.5):
    # Define loss function
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_labels, logits=y_output))
    #train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
    train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)

    # Calculate accuracy
    correct_prediction = tf.equal(tf.argmax(y_output,1), tf.argmax(y_labels,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    return train_step, accuracy

def train_test_model(mnist, x_input, y_labels, accuracy, train_step, steps=1000, batch=100):
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    for i in range(steps):
      input_batch, labels_batch = mnist.train.next_batch(batch)
      feed_dict = {x_input: input_batch, y_labels: labels_batch}

      if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict=feed_dict)
        print("Step %d, training batch accuracy %g"%(i, train_accuracy))

      train_step.run(feed_dict=feed_dict)

    print("The end of training!")

    print("Test accuracy: %g"%accuracy.eval(feed_dict={x_input: mnist.test.images, y_labels: mnist.test.labels}))

然后我尝试在训练网络时使用它。 首先,我使用非常简单的1输出层:

import help
import tensorflow as tf
import numpy as np

# Read mnist data
mnist = help.read_mnist()

image_size = 28
labels_size = 10

# Input layer - flattened images
x_input = tf.placeholder(tf.float32, [None, image_size*image_size])
y_labels = tf.placeholder(tf.float32, [None, labels_size])

# Layers:
# - Input
# - Output (Dense)

# Output dense layer
y_output = tf.layers.dense(inputs=x_input, units=labels_size)

# Define training
train_step, accuracy = help.build_training(y_labels, y_output)

# Run the training & test
help.train_test_model(mnist, x_input, y_labels, accuracy, train_step)

然后我添加另一个ReLU层:

# Hidden Layer
hidden = tf.layers.dense(inputs=x_input, units=hidden_size, activation=tf.nn.relu)

# Output dense layer
y_output = tf.layers.dense(inputs=hidden, units=labels_size)

我两次都出现Segmentation fault错误。 我尝试了一些我在网上找到的东西,比如重新排序numpy和tensorflow导入子句,将help.py代码放在与网络架构和培训过程相同的文件中,或者增加docker镜像的内存。没有任何效果。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我升级到TensorFlow的下一个版本,版本1.2。它解决了所有问题。