我有2D图像的数据集X,并将其分为批次X_1,...,X_k。然后,对于批处理元素的每个第i个组件,我想将其乘以参数gamma_i并将beta_i添加到它们中。
错误:
Input reshape: (4, 12, 12, 1)
Traceback (most recent call last):
File "3.py", line 61, in <module>
model = build_test((i, 12, 12, 1))
File "3.py", line 49, in build_test
x = Lambda(lambda tensor, mask: tf.math.multiply(tensor, tf.convert_to_tensor(mask, dtype=tf.float32)))([input_img, gamma])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 634, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py", line 785, in call
return self.function(inputs, **arguments)
File "3.py", line 49, in <lambda>
x = Lambda(lambda tensor, mask: tf.math.multiply(tensor, tf.convert_to_tensor(mask, dtype=tf.float32)))([input_img, gamma])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1087, in convert_to_tensor
return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1145, in convert_to_tensor_v2
as_ref=False)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1224, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 305, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 246, in constant
allow_broadcast=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 284, in _constant_impl
allow_broadcast=allow_broadcast))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 454, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.
您可以使用以下代码重现该错误:
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Input, BatchNormalization
from tensorflow.keras.layers import Lambda, Multiply, Add, Reshape
from tensorflow.keras.models import Model
import tensorflow.keras.backend as K
import numpy as np
def build_test(input_shape):
input_img = Input(shape=input_shape)
print('Input reshape:', input_shape)
x = Lambda(lambda tensor, mask: tf.math.multiply(tensor, tf.convert_to_tensor(mask, dtype=tf.float32)))([input_img, gamma])
print('multiply:', x.shape)
x = Lambda(lambda tensor, mask: tf.math.add(tensor, tf.convert_to_tensor(mask, dtype=tf.float32)))([x, beta])
print('add:', x.shape)
test = Model(inputs=input_img, outputs=x)
return test
batch_sizes = [4, 8]
for i in batch_sizes:
gamma = np.arange(i)
beta = np.arange(i)
model = build_test((i, 12, 12, 1))
但是,当我使用下面的代码间接解决我的目的时,效果很好:
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Input, BatchNormalization
from tensorflow.keras.layers import Lambda, Multiply, Add, Reshape
from tensorflow.keras.models import Model
import tensorflow.keras.backend as K
import numpy as np
def build_test(input_shape):
input_img = Input(shape=input_shape)
print('Input reshape:', input_shape)
for batch in range(input_shape[0]):
# x = tf.gather(x, batch)
# print('tensor shape:', x.shape)
mask = np.empty(input_shape, dtype=np.float32)
# for gamma
mask.fill(1)
mask[batch].fill(gamma[batch])
x = Lambda(lambda tensor: tf.math.multiply(tensor, tf.convert_to_tensor(mask, dtype=tf.float32)))(input_img)
print('multiply:', x.shape)
# for beta
mask.fill(1)
mask[batch].fill(beta[batch])
x = Lambda(lambda tensor: tf.math.add(tensor, tf.convert_to_tensor(mask, dtype=tf.float32)))(x)
print('add:', x.shape)
test = Model(inputs=input_img, outputs=x)
return test
batch_sizes = [4, 8]
for i in batch_sizes:
gamma = np.arange(i)
beta = np.arange(i)
model = build_test((i, 12, 12, 1))
在我收到错误的以前的代码中我在做什么错?
答案 0 :(得分:1)
在以前的代码中,初始化时未事先声明mask,因此其张量值为空并给出错误。
由于在后面的代码中,您已经对其进行了初始化,所以效果很好。