TensorFlow InvalidArgumentError/Value error occurs with small change of code
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import numpy as np import tensorflow as tf import pandas as pd from sklearn.model_selection import train_test_split x_data = np.linspace(0, 1000000, 1000) y_true = np.sin(x_data) y_true += np.random.randn(len(x_data)) feature_columns = [tf.feature_column.numeric_column('x', shape=[1])] estimator = tf.estimator.DNNRegressor(feature_columns=feature_columns, hidden_units=[10,10,10], optimizer=lambda: tf.train.AdamOptimizer( learning_rate=0.1 )) X_train, X_test, y_train, y_test = train_test_split(x_data, y_true, test_size=0.3) input_function = tf.estimator.inputs.numpy_input_fn({'x': X_train},y_train, batch_size=8, num_epochs=None, shuffle=True) train_input_function = tf.estimator.inputs.numpy_input_fn({'x': X_train},y_train, batch_size=8, num_epochs=1000, shuffle=False) test_input_function = tf.estimator.inputs.numpy_input_fn({'x': X_test},y_test, batch_size=8, num_epochs=1000, shuffle=False) estimator.train(input_fn=input_function, steps=1000) train_metrics = estimator.evaluate(input_fn=train_input_function, steps=1000) test_metrics = estimator.evaluate(input_fn=test_input_function, steps=1000) print('TRAINING DATA METRICS') print(train_metrics) print() print('TEST DATA METRICS') print(test_metrics) print() |
效果很好。
但是,如果我将
Traceback (most recent call last):
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1576, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [1000], [1,700], [1,700], [1].During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File"C:/Users/Admin/Documents/PycharmProjects/TF_API_2/api.py", line 21, in
X_train, X_test, y_train, y_test = train_test_split(x_data, y_true, test_size=0.3)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection_split.py", line 2059, in train_test_split
safe_indexing(a, test)) for a in arrays))
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection_split.py", line 2059, in
safe_indexing(a, test)) for a in arrays))
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils__init__.py", line 162, in safe_indexing
return X[indices]
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\array_ops.py", line 524, in _slice_helper
name=name)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\array_ops.py", line 690, in strided_slice
shrink_axis_mask=shrink_axis_mask)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 10187, in strided_slice
name=name)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
return func(*args, **kwargs)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
op_def=op_def)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1731, in init
control_input_ops)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1579, in _create_c_op
raise ValueError(str(e))
ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [1000], [1,700], [1,700], [1].
如果我使用** 2而不是tf.square,代码也无法编译,错误:
错误:tensorflow:模型与损失= NaN分歧。
Traceback(最近一次调用最后一次):
File"C:/Users/Admin/Documents/PycharmProjects/TF_API_2/api.py", line 35, in
estimator.train(input_fn=input_function, steps=1000)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 376, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1145, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1173, in _train_model_default
saving_listeners)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1451, in _train_with_estimator_spec
_, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 583, in run
run_metadata=run_metadata)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1059, in run
run_metadata=run_metadata)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1150, in run
raise six.reraise(*original_exc_info)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\six.py", line 693, in reraise
raise value
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1135, in run
return self._sess.run(*args, **kwargs)
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1215, in run
run_metadata=run_metadata))
File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 635, in after_run
raise NanLossDuringTrainingError
tensorflow.python.training.basic_session_run_hooks.NanLossDuringTrainingError: NaN loss during training.
这一行[
这里有两个不同的问题:
#1,不要将numpy和tensorflow操作混合在一起。 除非您在急切的执行模式下评估图表,否则它们几乎永远不会在一起。
#2,当你的网络在几次迭代后产生NaN时,这通常是一个好的迹象,你正在遇到数字溢出/下溢。 在这种情况下,罪魁祸首是