我正在尝试在Jupyter Notebook上使用tensorflow和gpu实现一个简单的神经网络,但每次创建会话都失败了,我已经多次跟踪代码并将迭代次数减少到10次,并且还减少了输入tain和测试数据,仅用于测试它是否是计算能力的问题。我的网络有2个隐藏层;第一个包含3个神经元,第二个包含2个神经元。 这是我的代码:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import math
import tensorflow as tf
from tensorflow.python.framework import ops
from preprocessing import load_dataset
def model(X_train, Y_train, X_test, Y_test, learning_rate=0.0001):
"""
Arguments:
X_train -- training set, of shape (input size , number of training examples )
Y_train -- test set, of shape (output size , number of training examples )
X_test -- training set, of shape (input size , number of test examples )
Y_test -- test set, of shape (output size, number of test examples )
learning_rate -- learning rate of the optimization
Returns:
parameters -- parameters learnt by the model.
"""
ops.reset_default_graph()
X, Y = create_placeholders()
parameters = initialize_parameters()
Z3 = forward_propagation(X, parameters)
cost = compute_cost(Z3, Y)
print("X = " + str(X))
print("Y = " + str(Y))
print("Z3 = " + str(Z3))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(10):
print(sess.run([optimizer, cost], feed_dict={X: X_train, Y: Y_train}))
parameters = sess.run(parameters)
print("Parameters have been trained!")
correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
return parameters
X_train, Y_train, X_test, Y_test=load_dataset()
parameters= model(X_train, Y_train, X_test, Y_test)
以下是我正在调用的函数
def create_placeholders():
X = tf.placeholder(dtype=tf.float32, shape=(28755648, 5), name="X")
Y = tf.placeholder(dtype=tf.float32, shape=(1, 5), name="Y")
return X, Y
def initialize_parameters():
W1 = tf.get_variable("W1", [3, 28755648], initializer=tf.contrib.layers.xavier_initializer(seed=0))
b1 = tf.get_variable("b1", [3, 1], initializer=tf.zeros_initializer())
W2 = tf.get_variable("W2", [2, 3], initializer=tf.contrib.layers.xavier_initializer(seed=0))
b2 = tf.get_variable("b2", [2, 1], initializer=tf.zeros_initializer())
W3 = tf.get_variable("W3", [1, 2], initializer=tf.contrib.layers.xavier_initializer(seed=0))
b3 = tf.get_variable("b3", [1, 1], initializer=tf.zeros_initializer())
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2,
"W3": W3,
"b3": b3}
return parameters
def forward_propagation(X, parameters):
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
Z1 = tf.add(tf.matmul(W1, X), b1)
A1 = tf.nn.relu(Z1)
Z2 = tf.add(tf.matmul(W2, A1), b2)
A2 = tf.nn.relu(Z2)
Z3 = tf.add(tf.matmul(W3, A2), b3)
return Z3
def compute_cost(Z3, Y):
logits = Z3
labels = Y
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels))
return cost
每次运行代码时都会出现以下错误:
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
<ipython-input-42-9ce12ddb96d9> in <module>()
----> 1 parameters= model(X_train, Y_train, X_test, Y_test)
<ipython-input-38-ab5d84d97720> in model(X_train, Y_train, X_test, Y_test, learning_rate)
42
43 # Start the session to compute the tensorflow graph
---> 44 with tf.Session() as sess:
45
46 # Run the initialization
C:\Users\Chaymae\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in __init__(self, target, graph, config)
1480
1481 """
-> 1482 super(Session, self).__init__(target, graph, config=config)
1483 # NOTE(mrry): Create these on first `__enter__` to avoid a reference cycle.
1484 self._default_graph_context_manager = None
C:\Users\Chaymae\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in __init__(self, target, graph, config)
620 # pylint: enable=protected-access
621 else:
--> 622 self._session = tf_session.TF_NewDeprecatedSession(opts, status)
623 finally:
624 tf_session.TF_DeleteSessionOptions(opts)
C:\Users\Chaymae\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
471 None, None,
472 compat.as_text(c_api.TF_Message(self.status.status)),
--> 473 c_api.TF_GetCode(self.status.status))
474 # Delete the underlying status object from memory otherwise it stays alive
475 # as there is a reference to status from this from the traceback due to
InternalError: Failed to create session.
另一方面,我尝试创建新会话并运行一个简单的张量流图,它运行良好。 如果有人能帮助我,我会很高兴
答案 0 :(得分:-1)
在创建tf.reset_default_graph()
之前设置tf.Session()
应解决问题。