python - 无法在文件 tensorflow/core/kernels/mkl_relu_op.cc:873 中初始化内存原语描述符
问题描述
我试图复制一个实现自适应渐变的代码
错误来自此代码
for i, n_hidden in enumerate(n_hiddens):
if i == 0:
input = x
input_dim = n_in
else:
input = output
input_dim = n_hiddens[i - 1]
W = weight_variable([input_dim, n_hidden])
b = bias_variable([n_hidden])
h = tf.nn.relu(tf.matmul(input, W) + b)
output = tf.nn.dropout(h, keep_prob)
在
h = tf.nn.relu(tf.matmul(input, w) + b)
翻译说
AbortedError (see above for traceback): Operation received an exception:Status: 3, message: could not initialize a memory primitive descriptor, in file tensorflow/core/kernels/mkl_relu_op.cc:873
[[{{node Relu}} = _MklRelu[T=DT_FLOAT, _kernel="MklOp", _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, DMT/_0)]]
也有其他错误,但同样会发生错误。
我真的很擅长复制这段代码,所以我认为代码没有什么特别的错误,但我无法确认。我认为 tensorflow 附带的“relu”功能有问题,但我不知道该怎么做。
这是我的完整代码,如果你需要
import numpy as np
import tensorflow as tf
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
np.random.seed(0)
tf.set_random_seed(1234)
def inference(x, keep_prob, n_in, n_hiddens, n_out):
def weight_variable(shape):
initial = np.sqrt(2.0 / shape[0]) * tf.truncated_normal(shape)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.zeros(shape)
return tf.Variable(initial)
for i, n_hidden in enumerate(n_hiddens):
if i == 0:
input = x
input_dim = n_in
else:
input = output
input_dim = n_hiddens[i - 1]
W = weight_variable([input_dim, n_hidden])
b = bias_variable([n_hidden])
h = tf.nn.relu(tf.matmul(input, W) + b)
output = tf.nn.dropout(h, keep_prob)
W_out = weight_variable([n_hiddens[-1], n_out])
b_out = bias_variable([n_out])
y = tf.nn.softmax(tf.matmul(output, W_out) + b_out)
return y
def loss(y, t):
cross_entropy = tf.reduce_mean(-tf.reduce_sum(t * tf.log(tf.clip_by_value(y, 1e-10, 1.0)), reduction_indices=[1]))
return cross_entropy
def training(loss):
optimizer = tf.train.AdagradOptimizer(0.01)
train_step = optimizer.minimize(loss)
return train_step
def accuracy(y, t):
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(t, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
return accuracy
if __name__ == '__main__':
mnist = datasets.fetch_mldata('MNIST original')
n = len(mnist.data)
N = 30000
N_train = 20000
N_validation = 4000
indices = np.random.permutation(range(n))[:N]
X = mnist.data[indices]
X = X / 255.0
X = X - X.mean(axis=1).reshape(len(X), 1)
y = mnist.target[indices]
Y = np.eye(10)[y.astype(int)]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=N_train)
X_train, X_validation, Y_train, Y_validation = train_test_split(X_train, Y_train, test_size=N_validation)
n_in = len(X[0])
n_hiddens = [200, 200, 200]
n_out = len(Y[0])
p_keep = 0.5
x = tf.placeholder(tf.float32, shape=[None, n_in])
t = tf.placeholder(tf.float32, shape=[None, n_out])
keep_prob = tf.placeholder(tf.float32)
y = inference(x, keep_prob, n_in=n_in, n_hiddens=n_hiddens, n_out=n_out)
loss = loss(y, t)
train_step = training(loss)
accuracy = accuracy(y, t)
history = {'val_loss' : [], 'val_acc' : []}
epochs = 50
batch_size = 200
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
n_batches = N_train // batch_size
for epoch in range(epochs):
X_, Y_ = shuffle(X_train, Y_train)
for i in range(n_batches):
start = i * batch_size
end = start + batch_size
sess.run(train_step, feed_dict = {x : X_[start:end], t : Y_[start:end], keep_prob : p_keep})
val_loss = loss.eval(session=sess, feed_dict = {x : X_validation, t : Y_validation, keep_prob : 1.0})
val_acc = accuracy.eval(session = sess, feed_dict = {x : X_validation, t : Y_validation, keep_prob : 1.0})
history['val_loss'].append(val_loss)
history['val_acc'].append(val_acc)
print('epoch : ', epoch, ' validation loss : ', val_loss, ' validation accuracy : ', val_acc)
plt.rc('font', family = 'serif')
fig = plt.figure()
plt.plot(range(epochs), history['val_loss'], label = 'loss', color = 'black')
plt.xlabel('epochs')
plt.show()
accuracy_rate = accuracy.eval(session = sess, feed_dict = {x : X_test, t : Y_test, keep_prob : 1.0})
print('accuracy : ', accuracy_rate)
这是我的整个错误日志
/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/sklearn/model_selection/_split.py:2069: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.
FutureWarning)
2018-11-05 18:11:21.403663: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-11-05 18:11:21.406748: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
2018-11-05 18:11:22.406667: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at mkl_relu_op.cc:876 : Aborted: Operation received an exception:Status: 3, message: could not initialize a memory primitive descriptor, in file tensorflow/core/kernels/mkl_relu_op.cc:873
Traceback (most recent call last):
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1292, in _do_call
return fn(*args)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1277, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1367, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.AbortedError: Operation received an exception:Status: 3, message: could not initialize a memory primitive descriptor, in file tensorflow/core/kernels/mkl_relu_op.cc:873
[[{{node Relu}} = _MklRelu[T=DT_FLOAT, _kernel="MklOp", _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, DMT/_0)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/shin/pythonClass/AdaGrad.py", line 105, in <module>
sess.run(train_step, feed_dict = {x : X_[start:end], t : Y_[start:end], keep_prob : p_keep})
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 887, in run
run_metadata_ptr)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1110, in _run
feed_dict_tensor, options, run_metadata)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1286, in _do_run
run_metadata)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1308, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.AbortedError: Operation received an exception:Status: 3, message: could not initialize a memory primitive descriptor, in file tensorflow/core/kernels/mkl_relu_op.cc:873
[[{{node Relu}} = _MklRelu[T=DT_FLOAT, _kernel="MklOp", _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, DMT/_0)]]
Caused by op 'Relu', defined at:
File "/home/shin/pythonClass/AdaGrad.py", line 81, in <module>
y = inference(x, keep_prob, n_in=n_in, n_hiddens=n_hiddens, n_out=n_out)
File "/home/shin/pythonClass/AdaGrad.py", line 31, in inference
h = tf.nn.relu(tf.matmul(input, W) + b)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 6780, in relu
"Relu", features=features, name=name)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3272, in create_op
op_def=op_def)
File "/home/shin/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1768, in __init__
self._traceback = tf_stack.extract_stack()
AbortedError (see above for traceback): Operation received an exception:Status: 3, message: could not initialize a memory primitive descriptor, in file tensorflow/core/kernels/mkl_relu_op.cc:873
[[{{node Relu}} = _MklRelu[T=DT_FLOAT, _kernel="MklOp", _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, DMT/_0)]]
解决方案
推荐阅读
- postgresql - postgres 函数看不到变量
- python - 从拟合的 xgboost 模型对象中检索超参数
- javascript - 跳过图表 js 中没有值的日期
- python - 让用户选择要重新采样的时间范围
- python - Plotly/Jupyter 中的 LaTeX 无法在 Firefox 上运行
- postgresql - 如果需要,使用 pg_ctl 启动 Postgres DB
- db2 - IBM CDC 表应该已经被刷新。转换服务器将终止
- css - 使用 React 自定义元素时不应用 styled-components 样式
- javascript - 如何在jsPsych的canvas-slider-response插件中隐藏滑块的拇指直到参与者响应?
- perl - 循环遍历 perl 数组