当我定义执行神经网络训练的函数时,出现错误。但是,当我没有使其运行而执行此操作时,就没有错误。为什么?
def myneural():
import numpy as np
import keras
from keras import backend as K
from keras.layers import Input, Dense, Activation
from keras.models import Model, Sequential
x_train_s = np.random.randint(5, size=(20, 2))
x_test_s = x_train_s
model = Sequential([
Dense(10, input_shape=(2,)),
Activation('linear'),
Dense(2),
Activation('linear')
])
model.compile(optimizer='adam', loss='mean_squared_error')
fittingadam = model.fit(x_train_s, x_train_s, epochs=2,
validation_data=(x_test_s, x_test_s),
shuffle=True, verbose=1, batch_size=None)
encoder = K.function([model.layers[0].input],
[model.layers[1].output])
code = encoder([x_test_s])[0]
myneural()
我得到的错误是:
Using TensorFlow backend.
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Train on 20 samples, validate on 20 samples
Epoch 1/2
2019-10-03 14:34:50.275279: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
20/20 [==============================] - 0s 7ms/step - loss: 4.0432 - val_loss: 3.9670
Epoch 2/2
20/20 [==============================] - 0s 53us/step - loss: 3.9670 - val_loss: 3.8917
Exception ignored in: <function BaseSession._Callable.__del__ at 0x0000021A829C1378>
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1453, in __del__
AttributeError: 'NoneType' object has no attribute 'raise_exception_on_not_ok_status'
Process finished with exit code 0
但是,当我删除第一行和最后一行并从其他行中删除缩进时,不会有错误。
第一个问题:为什么?我该如何解决它的功能而没有任何问题?
第二个问题:警告如何?他们重要吗?我该如何摆脱它们?
答案 0 :(得分:1)
因此,一般来说:只要它能满足您的要求,并且结尾处带有exit code 0
,请忽略所有警告和异常。