我读了这个https://www.tensorflow.org/guide/keras/custom_callback,但我不知道如何获取所有其他参数。
这是我的代码
(hits, ndcgs) = evaluate_model(model, testRatings, testNegatives, topK, evaluation_threads)
hr, ndcg, loss = np.array(hits).mean(), np.array(ndcgs).mean(), hist.history['loss'][0]
print('Iteration %d [%.1f s]: HR = %.4f, NDCG = %.4f, loss = %.4f [%.1f s]'
% (epoch, t2-t1, hr, ndcg, loss, time()-t2))
if hr > best_hr:
best_hr, best_ndcg, best_iter = hr, ndcg, epoch
if args.out > 0:
model.save(model_out_file, overwrite=True)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的model,我需要hist和model.save。有没有办法在自定义回调中使用这三个参数?这样我就可以将所有这些写入自定义回调中。
class CustomCallback(keras.callbacks.Callback):
def on_epoch_end(self, logs=None):
keys = list(logs.keys())
print("Stop training; got log keys: {}".format(keys))
Run Code Online (Sandbox Code Playgroud) 如何添加 Keras dropout 层?不幸的是,我不知道到底应该在哪里添加这一层。我看了2个链接:
例如,我见过这个
model.add(Dense(60, input_dim=60, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Dense(30, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
Run Code Online (Sandbox Code Playgroud)
据我了解,密集层是用循环创建的,所以我不知道如何添加它。
def get_Model(...):
# build dense layer for model
for i in range(1, len(dense_layers)):
layer = Dense(dense_layers[i],
activity_regularizer=l2(reg_layers[i]),
activation='relu',
name='layer%d' % i)
mlp_vector = layer(mlp_vector)
predict_layer = Concatenate()([mf_cat_latent, mlp_vector])
result = Dense(1, activation='sigmoid',
kernel_initializer='lecun_uniform', name='result')
model = Model(inputs=[input_user, input_item], outputs=result(predict_layer))
return model
Run Code Online (Sandbox Code Playgroud) 如何在不更改顺序的情况下删除重复的二维列表?
list_j = [[100,2,3,3], [4,98,99,98], [5,99,98,4], [5,99,98,5], [100,99,98,100,100,6]]
list_pre = [list(set(i)) for i in list_j]
print(list_pre)
[[2, 3, 100], [98, 99, 4], [98, 99, 4, 5], [98, 99, 5], [98, 99, 100, 6]]
Run Code Online (Sandbox Code Playgroud)
如您所见,它更改了顺序。我想要的是[[100,2,3,],...]
期望输出 [[100,2,3,], [4,98,99], [5,99,98,4], [5,99,98], [100,99,98,6]]