【问题标题】:TypeError: ('Keyword argument not understood:', 'training')TypeError:('关键字参数不理解:','培训')
【发布时间】:2021-08-17 17:18:32
【问题描述】:
IMAGE_RES = 224
def format_image(image, label):
  image = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))/255.0
  return image, label
BATCH_SIZE = 32
train_batches = train_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
train_gray_batches = train_grey_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
test_batches = test_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
test_grey_batches = test_grey_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
----------

threshold = 100.0
dropoutrate = 0.5
n_outchannels = 3
height, width = IMAGE_RES, IMAGE_RES
def max_norm_regularizer(threshold, axes=None, name="max_norm",
                         collection="max_norm"):
    def max_norm(weights):
        clipped = tf.clip_by_norm(weights, clip_norm=threshold, axes=axes)
        clip_weights = tf.assign(weights, clipped, name=name)
        tf.add_to_collection(collection, clip_weights)
        return None # there is no regularization loss term
    return max_norm

max_norm_reg = max_norm_regularizer(threshold=threshold)
clip_all_weights = tf.compat.v1.get_collection("max_norm")
----------
def leaky_relu(z,name=None):
    return tf.maximum(0.5*z,z,name=name)
from functools import partial
he_init = tf.keras.initializers.VarianceScaling()
----------
    X = tf.compat.v1.placeholder(shape=(None,width,height,2),dtype=tf.float32)
    print(X)
    training = tf.compat.v1.placeholder_with_default(False,shape=(),name='training')
    
    X_drop = tf.keras.layers.Dropout(X,dropoutrate)
    my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization,training=training,momentum=0.9)
    bn0 = my_batch_norm_layer(X_drop)
    bn0_act = leaky_relu(bn0)
    print(bn0_act)

此错误在我的程序中创建。我没有什么问题 明白要解决这个问题我搜索了很多次但没有解决这个问题 有问题吗?

Tensor("Placeholder_26:0", shape=(?, 224, 224, 2), dtype=float32)

TypeError                                 Traceback (most recent call last)
<ipython-input-64-adf525e2e2de> in <module>()
      5 X_drop = tf.keras.layers.Dropout(X,dropoutrate)
      6 my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization,training=training,momentum=0.9)
----> 7 bn0 = my_batch_norm_layer(X_drop)
      8 bn0_act = leaky_relu(bn0)
      9 print(bn0_act)

4 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/generic_utils.py

在 validate_kwargs(kwargs, allowed_kwargs, error_message) 1135 for kwarg in kwargs: 1136 如果 kwarg 不在 allowed_kwargs 中: -> 1137 引发类型错误(error_message,kwarg) 1138 第1139章

TypeError: ('Keyword argument not understood:', 'training')

【问题讨论】:

标签: python-3.x tensorflow keras neural-network


【解决方案1】:

您需要将参数放在括号内,因为training 关键字当前正应用于partial()。您还想使用trainable 而不是training(我假设您想冻结batchnorm 层)。

X = tf.compat.v1.placeholder(shape=(None,width,height,2),dtype=tf.float32)
print(X)
training = tf.compat.v1.placeholder_with_default(False,shape=(),name='training')

X_drop = tf.keras.layers.Dropout(dropoutrate)(X)
my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization(trainable=training,momentum=0.9))
bn0 = my_batch_norm_layer(X_drop)
bn0_act = leaky_relu()(bn0)
print(bn0_act)

【讨论】:

  • my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization(training=training,momentum=0.9)),这里也出错 TypeError: ('Keyword argument not understand:', 'training')跨度>
  • TypeError:您正试图在未声明为动态的层中使用 Python 控制流。将dynamic=True 传递给类构造函数。遇到的错误:Graph执行中不允许使用tf.Tensor作为Python bool的“””。使用Eager execution或用@tf.function修饰此函数。再次错误6字符串
  • 除非您提供完整代码,否则我无法修复这些错误
  • 我添加了我之前使用的功能
  • colab.research.google.com/drive/… ,其余鳕鱼的链接
猜你喜欢
  • 2020-12-09
  • 1970-01-01
  • 2020-12-09
  • 2020-10-19
  • 2018-12-29
  • 2021-01-28
  • 1970-01-01
相关资源
最近更新 更多