python - reshape 的输入是一个有 128 个值的张量,但请求的形状有 32 个 [Op:Reshape]
问题描述
这让我有点难过。我尝试了几种选择,但无法使其正常工作。我正在做的是对我自己的数据使用 TF2.0 三重损失函数,但它无法正常工作。我使用 TF 中的示例进行初始测试,并且有效。
功能链接供参考:https ://www.tensorflow.org/addons/tutorials/losses_triplet
有一点不同,我在 Windows 上,所以我无法下载和安装 tensorflow-addons,所以我抓住了使代码工作所需的东西。我使用相同的模型并以相同的方式编译它,但仍然有问题。这是代码:
augment = True
if augment:
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0,
rotation_range=20,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
validation_split=0.20) # set validation split
else:
train_datagen = ImageDataGenerator(
horizontal_flip=True,
rescale=1. / 255,
fill_mode='nearest',
validation_split=0.20) # set validation split
train_generator = train_datagen.flow_from_directory(
DATA_PATH,
target_size=(28,28),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='training') # set as training data
validation_generator = train_datagen.flow_from_directory(
DATA_PATH, # same directory as training data
target_size=(28,28),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='validation') # set as validation data
# Build the network
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='valid', activation='relu', input_shape=(28,28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='valid', activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256, activation=None)) # No activation on final dense layer
model.add(tf.keras.layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))) # L2 normalize embeddings
# Compile the model
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss=triplet_semihard_loss)
它都一样,只是不是数据。我什至调整数据的大小和灰度,使其与其他输入数据的形状相同。
这是整个错误:
File "C:\Users\matthew.millar\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 128 values, but the requested shape has 32 [Op:Reshape]
编辑
def triplet_semihard_loss(y_true, y_pred, margin=1.0):
labels, embeddings = y_true, y_pred
# Reshape label tensor to [batch_size, 1].
lshape = tf.shape(labels)
labels = tf.reshape(labels, [lshape[0], 1])
# Build pairwise squared distance matrix.
pdist_matrix = pairwise_distance(embeddings, squared=True)
# Build pairwise binary adjacency matrix.
adjacency = tf.math.equal(labels, tf.transpose(labels))
# Invert so we can select negatives only.
adjacency_not = tf.math.logical_not(adjacency)
batch_size = tf.size(labels)
# Compute the mask.
pdist_matrix_tile = tf.tile(pdist_matrix, [batch_size, 1])
mask = tf.math.logical_and(
tf.tile(adjacency_not, [batch_size, 1]),
tf.math.greater(pdist_matrix_tile,
tf.reshape(tf.transpose(pdist_matrix), [-1, 1])))
mask_final = tf.reshape(
tf.math.greater(
tf.math.reduce_sum(
tf.cast(mask, dtype=tf.dtypes.float32), 1, keepdims=True),
0.0), [batch_size, batch_size])
mask_final = tf.transpose(mask_final)
adjacency_not = tf.cast(adjacency_not, dtype=tf.dtypes.float32)
mask = tf.cast(mask, dtype=tf.dtypes.float32)
# negatives_outside: smallest D_an where D_an > D_ap.
negatives_outside = tf.reshape(
_masked_minimum(pdist_matrix_tile, mask), [batch_size, batch_size])
negatives_outside = tf.transpose(negatives_outside)
# negatives_inside: largest D_an.
negatives_inside = tf.tile(
_masked_maximum(pdist_matrix, adjacency_not), [1, batch_size])
semi_hard_negatives = tf.where(mask_final, negatives_outside,
negatives_inside)
loss_mat = tf.math.add(margin, pdist_matrix - semi_hard_negatives)
mask_positives = tf.cast(
adjacency, dtype=tf.dtypes.float32) - tf.linalg.diag(
tf.ones([batch_size]))
# In lifted-struct, the authors multiply 0.5 for upper triangular
# in semihard, they take all positive pairs except the diagonal.
num_positives = tf.math.reduce_sum(mask_positives)
triplet_loss = tf.math.truediv(
tf.math.reduce_sum(
tf.math.maximum(tf.math.multiply(loss_mat, mask_positives), 0.0)),num_positives)
return triplet_loss
这是整个堆栈跟踪:
Traceback (most recent call last):
File "C:/Users/gus/Documents/ImageSimularity/FoodTrainer.py", line 79, in <module>
callbacks=callbacks_list)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1297, in fit_generator
steps_name='steps_per_epoch')
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py", line 265, in model_iteration
batch_outs = batch_function(*batch_data)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 973, in train_on_batch
class_weight=class_weight, reset_metrics=reset_metrics)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 264, in train_on_batch
output_loss_metrics=model._output_loss_metrics)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 311, in train_on_batch
output_loss_metrics=output_loss_metrics))
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 252, in _process_single_batch
training=training))
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 166, in _model_loss
per_sample_losses = loss_fn.call(targets[i], outs[i])
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\losses.py", line 221, in call
return self.fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\gus\Documents\ImageSimularity\Tripleloss.py", line 75, in triplet_semihard_loss
labels = tf.reshape(labels, [lshape[0], 1])
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 131, in reshape
result = gen_array_ops.reshape(tensor, shape, name)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 8105, in reshape
tensor, shape, name=name, ctx=_ctx)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 8143, in reshape_eager_fallback
ctx=_ctx, name=name)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 128 values, but the requested shape has 32 [Op:Reshape]
Process finished with exit code 1
解决方案
tf.reshape
不会改变张量中元素的顺序或总数。错误表明,您正试图将元素总数从 128 个减少到 32 个。您正在使用tf.reshape
intriplet_semihard_loss
函数。
在下面的示例中,我重新创建了您的场景,其中给定的参数shape
不包含原始张量的所有元素,因此引发错误 -2
tf.reshape
代码 -
%tensorflow_version 2.x
import tensorflow as tf
t1 = tf.Variable([1,2,2,4,5,6])
t2 = tf.reshape(t1, 2)
输出 -
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-3-0ff1d701ff22> in <module>()
3 t1 = tf.Variable([1,2,2,4,5,6])
4
----> 5 t2 = tf.reshape(t1, 2)
3 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: Input to reshape is a tensor with 6 values, but the requested shape has 2 [Op:Reshape]
tf.reshape
应该以这样一种方式,即元素的排列可以改变,但元素的总数必须保持不变。所以解决方法是将形状更改为[2,3]
-
代码 -
%tensorflow_version 2.x
import tensorflow as tf
t1 = tf.Variable([1,2,2,4,5,6])
t2 = tf.reshape(t1, [2,3])
print(t2)
输出 -
tf.Tensor(
[[1 2 2]
[4 5 6]], shape=(2, 3), dtype=int32)
希望这能回答你的问题。快乐学习。
推荐阅读
- excel - 用于复制并粘贴到 BS 列的下一个空行的宏
- pybind11 - VS2017 与 pybind11 一起使用时会引发链接器错误
- c# - 等待第三方 API 回调
- c# - UWP C#:从本地磁盘打开 HTML 文件到默认浏览器
- javascript - addEventListener 没有在按键上触发
- php - 如果身体标签通过子主题具有“家”类
- python - 用Python模拟退火算法解决旅行商问题
- python-3.x - 如何使用 Youtube Data API v3 获取 youtube 频道中的所有视频标题?
- google-admin-sdk - 查询 Admin Report API 以了解用户活动时未返回任何项目
- python - python shutil.move 使用文件修复移动目录