Я получил странную ошибку, используя Keras
и Tensorflow 2.1.0
. Я заметил, что при Tensorflow 2.2.0-rc2
ошибка не появляется.
Ошибка:
ValueError: веса не могут быть переданы значениям. values.rank = 0. weights.rank = 2. values.shape = (). weights.shape = (None, 20).
Полная трассировка стека ошибок:
Traceback (most recent call last):
File "lstm_keras.py", line 82, in <module>
model.compile(loss=listnet_loss, optimizer=keras.optimizers.Adam(learning_rate=0.00005, beta_1=0.9, beta_2=0.999, amsgrad=False))
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 446, in compile
self._compile_weights_loss_and_weighted_metrics()
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 1592, in _compile_weights_loss_and_weighted_metrics
self.total_loss = self._prepare_total_loss(masks)
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 1656, in _prepare_total_loss
reduction=losses_utils.ReductionV2.NONE)
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/losses_utils.py", line 107, in compute_weighted_loss
losses, sample_weight)
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/ops/losses/util.py", line 148, in scale_losses_by_sample_weight
sample_weight = weights_broadcast_ops.broadcast_weights(sample_weight, losses)
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/ops/weights_broadcast_ops.py", line 167, in broadcast_weights
with ops.control_dependencies((assert_broadcastable(weights, values),)):
File "/usr/home/studenti/sp171412/word_ordering/env/lib/python3.6/site-packages/tensorflow_core/python/ops/weights_broadcast_ops.py", line 103, in assert_broadcastable
weights_rank_static, values.shape, weights.shape))
ValueError: weights can not be broadcast to values. values.rank=0. weights.rank=2. values.shape=(). weights.shape=(None, 20).
Кажется, что ошибка связана с моей пользовательской функцией потерь. Как я уже говорил, ошибка не появляется в Tensorflow 2.2.0-rc2
Это код:
def get_top_one_probability(vector):
return (K.exp(vector) / K.sum(K.exp(vector)))
def listnet_loss(real_labels, predicted_labels):
return -K.sum(get_top_one_probability(real_labels) * tf.math.log(get_top_one_probability(predicted_labels)))
# Load dataset
training_dataset= []
with open('../datasets/en-train.H', 'r') as en_train:
for row in en_train:
row_data = row.split(',')
training_dataset.append(list(map(lambda element: float(element), row_data)))
training_dataset = np.array(training_dataset)
# Fit scaler with training set
scaler = StandardScaler()
scaler.fit(training_dataset[:,0:training_dataset.shape[1]-1])
device_lib.list_local_devices()
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
features = training_dataset.shape[1] - 2
timesteps = 20
# Model definition
model = keras.Sequential()
model.add(Masking(mask_value=0., input_shape=(timesteps, features)))
model.add(Bidirectional(LSTM(units=100, return_sequences=True), input_shape=(timesteps, features)))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss=listnet_loss, optimizer=keras.optimizers.Adam(learning_rate=0.00005, beta_1=0.9, beta_2=0.999, amsgrad=False))
number_of_epochs = 50
# Training phase
with tf.device('/GPU:0'):
for epoch in range(0, 50):
# Reading dataset using pandas and chunking it
training_dataset_iterator = pd.read_csv('../datasets/en-train.H',
chunksize=80000, sep=",", header=None, dtype=np.float64)
for chunk in training_dataset_iterator:
training_set_portion = chunk.values
training_set_portion_labels = training_set_portion[:, training_set_portion.shape[1]-1]
portion_groups = get_groups_id_count(training_set_portion[:,0])
# Scale dataset portion
training_set_portion = scaler.transform(training_set_portion[:,0:training_set_portion.shape[1]-1])
training_set_portion = np.concatenate((training_set_portion, training_set_portion_labels[:, np.newaxis]), axis=1)
# Pad dataset portion
training_set_portion = pad_groups(training_set_portion, portion_groups)
training_set_portion_labels = training_set_portion[:, training_set_portion.shape[1]-1]
# Exluding group and label from training_set_portion
training_set_portion = training_set_portion[:, 1:training_set_portion.shape[1] - 1]
# Reshape data for LSTM
training_set_portion = training_set_portion.reshape(int(training_set_portion.shape[0]/timesteps), timesteps, features)
training_set_portion_labels = training_set_portion_labels.reshape(int(training_set_portion_labels.shape[0]/timesteps), timesteps)
model.fit(training_set_portion, training_set_portion_labels, validation_split=0.2, shuffle=False, epochs=1,
batch_size=1, workers=0, max_queue_size=1, verbose=1)
model_json = model.to_json()
with open("./models_padded/model_{0}.json".format(epoch+1), "w+") as json_file:
json_file.write(model_json)
model.save_weights("./models_weights_padded/model_{0}_weights.h5".format(epoch+1))
print('Model at epoch {0} saved!\n\n'.format(epoch+1))
В чем проблема?