return (features, y) build_batch = (BuildBatch(BATCH_SIZE).by(0, 'vector', float).by(1, 'number', float)) if NET_ARCH == 'deep_net': model = deep_net(input_shape=(feature_dim, )) opti = Adam(lr=0.0001, beta_1=0.5) elif NET_ARCH == 'DNN': model = DNN(input_shape=(feature_dim, )) opti = sgd(lr=0.01, momentum=0.9, clipnorm=1.0) else: sys.exit("Network not defined correctly, check NET_ARCH. ") model.compile(optimizer=opti, loss='mean_squared_error', metrics=[Rsqured]) def train_network_batch(sample): tloss = model.train_on_batch(sample[0], sample[1]) return (tloss[0], tloss[1]) def test_network_batch(sample): tloss = model.test_on_batch(sample[0], sample[1]) return (tloss[0], ) def predict_network_batch(sample): return model.predict(sample[0]) scale_activators = lambda x: (x[0] * dataset_stats.loc[ dataset_name, 'std'] + dataset_stats.loc[dataset_name, 'mean'])
X_test, y_test = load_from_npz(X_data_name_2), load_from_npz(y_data_name_2) X_train, X_test = normalize(X_train, X_test) # %% DNN (only fully-connected layer) dnn = DNN() X_train = np.reshape(X_train, (len(X_train), len(X_train[1]) * len(X_train[2]) * 3)) X_test = np.reshape(X_test, (len(X_test), len(X_train[1]) * len(X_train[2]) * 3)) #optimization details adam = Adam(lr=lrf, decay=lr_decay) dnn.compile(loss='mean_squared_error', optimizer=adam, metrics=[RMSE]) for epoch in range(1, maxepoches): if epoch % 25 == 0 and epoch > 0: lrf /= 2 adam = Adam(lr=lrf, decay=lr_decay) dnn.compile(loss='mean_squared_error', optimizer=adam, metrics=[RMSE]) dnn.fit(X_train, y_train, epochs=epoch, initial_epoch=epoch - 1, shuffle=True, batch_size=batch_size, validation_data=(X_test, y_test),
K = 10 learning_rate = 0.001 epochs = 30 batch_size = 512 # ========================== Create dataset ======================= item_fea_col, train, val, test = create_implicit_ml_1m_dataset( file, trans_score, embed_dim, maxlen) train_X, train_y = train val_X, val_y = val # ============================Build Model========================== model = DNN(item_fea_col, maxlen, hidden_unit, activation, embed_reg) model.summary() # =========================Compile============================ model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate)) results = [] for epoch in range(1, epochs + 1): # ===========================Fit============================== t1 = time() model.fit( train_X, train_y, validation_data=(val_X, val_y), epochs=1, batch_size=batch_size, ) # ===========================Test============================== t2 = time() if epoch % 5 == 0: