簡體   English   中英

如何修復張量流“InvalidArgumentError:所有輸入的形狀必須匹配”

[英]How to fix tensorflow "InvalidArgumentError: Shapes of all inputs must match"

我正在嘗試通過順序 Keras 神經網絡運行小波重建數據集。 為了從訓練中獲得更好的結果,我正在嘗試構建一個僅關注波形的某些索引的自定義損失函數。 我打算創建一個神經網絡來插入剪切的波形,所以我只希望神經網絡通過比較波形的剪切段與實際輸出來計算損失。

我已經嘗試為我的自定義損失函數創建一個包裝器,以便我可以傳入一個額外的輸入參數。 然后我使用這個輸入參數來查找裁剪數據點的索引,並嘗試從 y_pred 和 y_true 收集這些索引。

這是模型實例化和訓練的地方:

x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.7)
_dim = len(x_train[0])

# define the keras model
model = Sequential()

# tanh activation allows for vals between -1 and 1 unlike relu
model.add(Dense(_dim*2, input_dim=_dim, activation=_activation))
model.add(Dense(_dim*2, activation=_activation))
model.add(Dense(_dim, activation=_activation))
# model.compile(loss=_loss, optimizer=_optimizer)
model.compile(loss=_loss, optimizer=_optimizer, metrics=[custom_loss_wrapper_2(x_train)])

print(model.summary())

# The patience parameter is the amount of epochs to check for improvement
early_stop = EarlyStopping(monitor='val_loss', patience=5)

# fit the model
history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=150, batch_size=15, callbacks=[early_stop])

這就是我的自定義損失函數所在的位置:

def custom_loss_wrapper_2(inputs):
# source: https://stackoverflow.com/questions/55445712/custom-loss-function-in-keras-based-on-the-input-data
# 2nd source: http://stackoverflow.com/questions.55597335/how-to-use-tf-gather-in-batch
def reindex(tensor_tuple):
    # unpack tensor tuple
    y_true = tensor_tuple[0]
    y_pred = tensor_tuple[1]
    t_inputs = K.cast(tensor_tuple[2], dtype='int64')
    t_max_indices = K.tf.where(K.tf.equal(t_inputs, K.max(t_inputs)))

    # gather the values from y_true and y_pred
    y_true_gathered = K.gather(y_true, t_max_indices)
    y_pred_gathered = K.gather(y_pred, t_max_indices)

    print(K.mean(K.square(y_true_gathered - y_pred_gathered)))

    return K.mean(K.square(y_true_gathered - y_pred_gathered))

def custom_loss(y_true, y_pred):
    # Step 1: "tensorize" the previous list
    t_inputs = K.variable(inputs)

    # Step 2: Stack tensors
    tensor_tuple = K.stack([y_true, y_pred, t_inputs], axis=1)

    vals = K.map_fn(reindex, tensor_tuple, dtype='float32')
    print('vals: ', vals)

    return K.mean(vals)

return custom_loss

我在嘗試自定義損失函數時收到以下錯誤消息:

Using TensorFlow backend.
WARNING: Logging before flag parsing goes to stderr.
W0722 15:28:20.239395 17232 deprecation_wrapper.py:119] From C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0722 15:28:20.252325 17232 deprecation_wrapper.py:119] From C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0722 15:28:20.253353 17232 deprecation_wrapper.py:119] From C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

W0722 15:28:20.280281 17232 deprecation_wrapper.py:119] From C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

W0722 15:28:20.293246 17232 deprecation_wrapper.py:119] From C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py:1521: The name tf.log is deprecated. Please use tf.math.log instead.

W0722 15:28:20.366046 17232 deprecation.py:323] From C:\Users\Madison\PycharmProjects\MSTS\Seismic_Analysis\ML\custom_loss.py:83: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Tensor("metrics/custom_loss/map/while/Mean:0", shape=(), dtype=float32)
vals:  Tensor("metrics/custom_loss/map/TensorArrayStack/TensorArrayGatherV3:0", shape=(1228,), dtype=float32)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 1002)              503004    
_________________________________________________________________
dense_2 (Dense)              (None, 1002)              1005006   
_________________________________________________________________
dense_3 (Dense)              (None, 501)               502503    
=================================================================
Total params: 2,010,513
Trainable params: 2,010,513
Non-trainable params: 0
_________________________________________________________________
None
W0722 15:28:20.467779 17232 deprecation_wrapper.py:119] From C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.

Train on 1228 samples, validate on 527 samples
Epoch 1/150
2019-07-22 15:28:20.606792: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "C:/Users/Madison/PycharmProjects/MSTS/Seismic_Analysis/ML/clipping_ml.py", line 172, in <module>
    main()
  File "C:/Users/Madison/PycharmProjects/MSTS/Seismic_Analysis/ML/clipping_ml.py", line 168, in main
    run_general()
  File "C:/Users/Madison/PycharmProjects/MSTS/Seismic_Analysis/ML/clipping_ml.py", line 156, in run_general
    _loss=_loss, _activation=_activation, _optimizer=_optimizer)
  File "C:/Users/Madison/PycharmProjects/MSTS/Seismic_Analysis/ML/clipping_ml.py", line 59, in build_clipping_model
    history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=150, batch_size=15, callbacks=[early_stop])
  File "C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\engine\training.py", line 1039, in fit
    validation_steps=validation_steps)
  File "C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\engine\training_arrays.py", line 199, in fit_loop
    outs = f(ins_batch)
  File "C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
    return self._call(inputs)
  File "C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
    fetched = self._callable_fn(*array_vals)
  File "C:\Users\Madison\PycharmProjects\MSTS\venv\lib\site-packages\tensorflow\python\client\session.py", line 1458, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.**InvalidArgumentError: Shapes of all inputs must match**: values[0].shape = [15,501] != values[2].shape = [1228,501]
     [[{{node metrics/custom_loss/stack}}]]

經過一番思考,我找到了我原來問題的答案。 我想我會把它貼在這里,以防將來它可能對某人有所幫助。 我遇到的問題與我提供的損失函數包裝器的輸入參數有關。 當我應該只傳遞批處理輸入時,我傳遞了整個輸入數組。 這是在函數調用期間通過發送 model.inputs 來完成的。 所以新的編譯行應該是這樣的:

model.compile(loss=_loss, optimizer=_optimizer, metrics=[custom_loss_wrapper_2(model.input)])

你能分享一個可運行但失敗的問題示例嗎? 即使只有幾個數據點。 現在看起來您的數據形狀不一致。 例如,一個小波比另一個長。 批次需要是同質的。 檢查這一點的一種方法是:

print(set(inp.shape for inp in inputs))

如果該集合包含多個元素,則可能需要擴充數據。

問題片段中的示例代碼

import numpy as np
from keras import backend as K
from keras.callbacks import EarlyStopping
from keras.layers import Dense, Activation
from keras.models import Sequential
from keras import optimizers
from sklearn.model_selection import train_test_split

_activation = Activation('softmax')
_optimizer = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

def custom_loss_wrapper_2(inputs):
    print("inputs {}".format(inputs.shape))
    # source: https://stackoverflow.com/questions/55445712/custom-loss-function-in-keras-based-on-the-input-data
    # 2nd source: http://stackoverflow.com/questions.55597335/how-to-use-tf-gather-in-batch
    def reindex(tensor_tuple):
        # unpack tensor tuple
        y_true = tensor_tuple[0]
        y_pred = tensor_tuple[1]
        t_inputs = K.cast(tensor_tuple[2], dtype='int64')
        t_max_indices = K.tf.where(K.tf.equal(t_inputs, K.max(t_inputs)))

        # gather the values from y_true and y_pred
        print("y_true {}".format(y_true.shape))
        print("y_pred {}".format(y_pred.shape))
        y_true_gathered = K.gather(y_true, t_max_indices)
        y_pred_gathered = K.gather(y_pred, t_max_indices)

        print(K.mean(K.square(y_true_gathered - y_pred_gathered)))

        return K.mean(K.square(y_true_gathered - y_pred_gathered))

    def custom_loss(y_true, y_pred):
        print("y_true2 {}".format(y_true.shape))
        print("y_pred2 {}".format(y_pred.shape))

        # Step 1: "tensorize" the previous list
        t_inputs = K.variable(inputs)

        # Step 2: Stack tensors
        tensor_tuple = K.stack([y_true, y_pred, t_inputs], axis=1)

        vals = K.map_fn(reindex, tensor_tuple, dtype='float32')
        print('vals: {}'.format(vals.shape))
        print('kvals: {}'.format(K.mean(vals).shape))
        return K.mean(vals, keepdims=True)

    return custom_loss

dataset_size = 100
dim = 501
X = np.random.rand(dataset_size, dim)
Y = np.random.rand(dataset_size, dim)

x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.7)
print(x_train.shape)
print(y_train.shape)

print(x_test.shape)
print(y_test.shape)

_dim = len(x_train[0])
print("_dim {}".format(_dim))
# define the keras model
model = Sequential()

_loss = custom_loss_wrapper_2(x_train)
_mmm = _loss

# tanh activation allows for vals between -1 and 1 unlike relu
model.add(Dense(_dim*2, input_shape=(_dim,), activation=_activation))
model.add(Dense(_dim*2, activation=_activation))
model.add(Dense(_dim, activation=_activation))
# model.compile(loss=_loss, optimizer=_optimizer)
model.compile(loss=_loss, optimizer=_optimizer, metrics=[_mmm])

print(model.summary())

# The patience parameter is the amount of epochs to check for improvement
early_stop = EarlyStopping(monitor='val_loss', patience=5)

# fit the model
history = model.fit(
    x_train,
    y_train,
    validation_data=(x_test, y_test),
    epochs=150,
    batch_size=10,
    callbacks=[early_stop])


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM