neural network gives TypeError: ('Keyword argument not understood:', 'training')
neural network gives TypeError: ('Keyword argument not understood:', 'training')
我试图用张量流概率训练一个 fcnn 模型,但我得到了一个我不明白的错误。神经网络是
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import timeit
import tensorflow as tf
from tqdm import tqdm_notebook as tqdm
import tensorflow_probability as tfp
from tensorflow.keras.callbacks import TensorBoard
import datetime,os
tfd = tfp.distributions
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
def normal_exp(params):
return tfd.Normal(loc=params[:,0:1], scale=tf.math.exp(params[:,1:2]))
def NLL(y, distr):
return -distr.log_prob(y)
def create_model():
return tf.keras.models.Sequential([
Input(shape=(1,)),
Dense(200,activation="relu"),
Dropout(0.1, training=True),
Dense(500,activation="relu"),
Dropout(0.1, training=True),
Dense(500,activation="relu"),
Dropout(0.1, training=True),
Dense(200,activation="relu"),
Dropout(0.1, training=True),
Dense(2),
tfp.layers.DistributionLambda(normal_exp, name='normal_exp')
])
def train_model():
model = create_model()
model.compile(Adam(learning_rate=0.0002), loss=NLL)
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m %d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
model.fit(x= X_train, y =y_train, epochs=1500, validation_data=(X_val, y_val), callbacks=[tensorboard_callback])
train_model()
而错误显示
`/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message) ``
`1172 for kwarg in kwargs:`
`1173 if kwarg not in allowed_kwargs:`
`-> 1174 raise TypeError(error_message, kwarg)`
`1175`
`1176`
`` TypeError: ('Keyword argument not understood:', 'training')`
我尝试修改在 Sequential() 中定义神经网络的方式,但我不知道问题出在哪里
在 Sequential API
中,您不能在图层输入中使用 trining=True
作为 **kwargs
。但是您可以像下面这样在 Functional API
中使用 training=True
:
x = Input(shape=(1,))
x = Dense(200,activation="relu")(x)
x = Dropout(0.1)(x, training=True)
x = Dense(2)(x)
out = tfp.layers.DistributionLambda(normal_exp, name='normal_exp')(x)
您在 Sequential API
中的正确代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import timeit
import tensorflow as tf
from tqdm import tqdm_notebook as tqdm
import tensorflow_probability as tfp
from tensorflow.keras.callbacks import TensorBoard
import datetime,os
tfd = tfp.distributions
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
def normal_exp(params):
return tfd.Normal(loc=params[:,0:1], scale=tf.math.exp(params[:,1:2]))
def NLL(y, distr):
return -distr.log_prob(y)
def create_model():
return tf.keras.models.Sequential([
Input(shape=(1,)),
Dense(200,activation="relu"),
Dropout(0.1),
Dense(500,activation="relu"),
Dropout(0.1),
Dense(500,activation="relu"),
Dropout(0.1),
Dense(200,activation="relu"),
Dropout(0.1),
Dense(2),
tfp.layers.DistributionLambda(normal_exp, name='normal_exp')
])
def train_model():
model = create_model()
model.compile(Adam(learning_rate=0.0002), loss=NLL)
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m %d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
model.fit(x= X_train, y =y_train, epochs=2, validation_data=(X_val, y_val), callbacks=[tensorboard_callback])
X_train = np.random.rand(10,1)
y_train = np.random.rand(10)
X_val = np.random.rand(10,1)
y_val = np.random.rand(10)
train_model()
输出:
Epoch 1/2
1/1 [==============================] - 1s 1s/step - loss: 1.1478 - val_loss: 1.0427
Epoch 2/2
1/1 [==============================] - 0s 158ms/step - loss: 1.1299 - val_loss: 1.0281
这是因为 Dropout
层没有 training
参数。使用 model.fit
时,training
将自动适当地设置为 True,在其他情况下,您可以在调用层时将 kwarg 显式设置为 True :
tf.keras.layers.Dropout(0.2, noise_shape=None, seed=None)(dense, training=True)
我试图用张量流概率训练一个 fcnn 模型,但我得到了一个我不明白的错误。神经网络是
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import timeit
import tensorflow as tf
from tqdm import tqdm_notebook as tqdm
import tensorflow_probability as tfp
from tensorflow.keras.callbacks import TensorBoard
import datetime,os
tfd = tfp.distributions
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
def normal_exp(params):
return tfd.Normal(loc=params[:,0:1], scale=tf.math.exp(params[:,1:2]))
def NLL(y, distr):
return -distr.log_prob(y)
def create_model():
return tf.keras.models.Sequential([
Input(shape=(1,)),
Dense(200,activation="relu"),
Dropout(0.1, training=True),
Dense(500,activation="relu"),
Dropout(0.1, training=True),
Dense(500,activation="relu"),
Dropout(0.1, training=True),
Dense(200,activation="relu"),
Dropout(0.1, training=True),
Dense(2),
tfp.layers.DistributionLambda(normal_exp, name='normal_exp')
])
def train_model():
model = create_model()
model.compile(Adam(learning_rate=0.0002), loss=NLL)
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m %d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
model.fit(x= X_train, y =y_train, epochs=1500, validation_data=(X_val, y_val), callbacks=[tensorboard_callback])
train_model()
而错误显示
`/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message) ``
`1172 for kwarg in kwargs:`
`1173 if kwarg not in allowed_kwargs:`
`-> 1174 raise TypeError(error_message, kwarg)`
`1175`
`1176`
`` TypeError: ('Keyword argument not understood:', 'training')`
我尝试修改在 Sequential() 中定义神经网络的方式,但我不知道问题出在哪里
在 Sequential API
中,您不能在图层输入中使用 trining=True
作为 **kwargs
。但是您可以像下面这样在 Functional API
中使用 training=True
:
x = Input(shape=(1,))
x = Dense(200,activation="relu")(x)
x = Dropout(0.1)(x, training=True)
x = Dense(2)(x)
out = tfp.layers.DistributionLambda(normal_exp, name='normal_exp')(x)
您在 Sequential API
中的正确代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import timeit
import tensorflow as tf
from tqdm import tqdm_notebook as tqdm
import tensorflow_probability as tfp
from tensorflow.keras.callbacks import TensorBoard
import datetime,os
tfd = tfp.distributions
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
def normal_exp(params):
return tfd.Normal(loc=params[:,0:1], scale=tf.math.exp(params[:,1:2]))
def NLL(y, distr):
return -distr.log_prob(y)
def create_model():
return tf.keras.models.Sequential([
Input(shape=(1,)),
Dense(200,activation="relu"),
Dropout(0.1),
Dense(500,activation="relu"),
Dropout(0.1),
Dense(500,activation="relu"),
Dropout(0.1),
Dense(200,activation="relu"),
Dropout(0.1),
Dense(2),
tfp.layers.DistributionLambda(normal_exp, name='normal_exp')
])
def train_model():
model = create_model()
model.compile(Adam(learning_rate=0.0002), loss=NLL)
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m %d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
model.fit(x= X_train, y =y_train, epochs=2, validation_data=(X_val, y_val), callbacks=[tensorboard_callback])
X_train = np.random.rand(10,1)
y_train = np.random.rand(10)
X_val = np.random.rand(10,1)
y_val = np.random.rand(10)
train_model()
输出:
Epoch 1/2
1/1 [==============================] - 1s 1s/step - loss: 1.1478 - val_loss: 1.0427
Epoch 2/2
1/1 [==============================] - 0s 158ms/step - loss: 1.1299 - val_loss: 1.0281
这是因为 Dropout
层没有 training
参数。使用 model.fit
时,training
将自动适当地设置为 True,在其他情况下,您可以在调用层时将 kwarg 显式设置为 True :
tf.keras.layers.Dropout(0.2, noise_shape=None, seed=None)(dense, training=True)