def transfer(source_model, target_path, frozen_layer): # load data of branch df = pd.read_pickle(target_path) # create dataframe with netto sales, month, weekday, year df = pd.DataFrame(data=df.values, index=df.index, columns=['netto']) df = df.assign(month=df.index.month) df = df.assign(weekday=df.index.weekday) df = df.assign(year=df.index.year) # split into train and test train, test = split_dataset(df.values, 365) # prepare input data for branch n_input = 365 train_x, train_y = to_supervised(train, n_input, 365) # load pre-trained model of source branch as base model base_model = load_model(source_model) # freeze specific layers of base model for layer in base_model.layers[:frozen_layer]: layer.trainable = False print("frozen layers: " + str(frozen_layer)) # compile the model base_model.compile(loss='mse', optimizer='adam') # fit base_model with new data from branch n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1] input_data = [train_x[:, :, i].reshape((train_x.shape[0], n_timesteps, 1)) for i in range(n_features)] base_model.fit(input_data, train_y, epochs=20, batch_size=16, verbose=0) # evaluate fitted model mape = evaluate_model(train, test, base_model) return mape
def build_model(train): # model parameters n_input = 365 epochs = 20 batch_size = 16 filters = 64 kernel_size = 8 pool_size = 4 dense_1 = 500 dense_2 = 200 # prepare data train_x, train_y = to_supervised(train, n_input, 365) # define parameters verbose = 2 n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[ 2], train_y.shape[1] # create a channel for each variable in_layers, out_layers = list(), list() for i in range(n_features): inputs = Input(shape=(n_timesteps, 1)) conv1 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(inputs) conv2 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(conv1) conv3 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(conv2) conv4 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(conv3) pool1 = MaxPooling1D(pool_size=pool_size)(conv4) flat = Flatten()(pool1) # store layers in_layers.append(inputs) out_layers.append(flat) # merge heads merged = concatenate(out_layers) # interpretation dense1 = Dense(dense_1, activation='relu')(merged) dense2 = Dense(dense_2, activation='relu')(dense1) outputs = Dense(n_outputs)(dense2) model = Model(inputs=in_layers, outputs=outputs) # compile model model.compile(loss='mse', optimizer='adam') # fit network input_data = [ train_x[:, :, i].reshape((train_x.shape[0], n_timesteps, 1)) for i in range(n_features) ] model.fit(input_data, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose) # model.summary() # added return model
def build_model(train, n_input): # prepare data train_x, train_y = to_supervised(train, n_input, 365) # define parameters epochs, batch_size, verbose = 20, 8, 0 n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[ 2], train_y.shape[1] # define model model = Sequential() model.add( Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(n_timesteps, n_features))) model.add(Conv1D(filters=32, kernel_size=3, activation='relu')) model.add(MaxPooling1D(pool_size=2)) model.add(Conv1D(filters=16, kernel_size=3, activation='relu')) model.add(Conv1D(filters=16, kernel_size=3, activation='relu')) model.add(MaxPooling1D(pool_size=2)) model.add(Flatten()) model.add(Dense(100, activation='relu')) model.add(Dense(n_outputs)) # compile model model.compile(loss='mse', optimizer='adam') # fit network model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose) # plot the model # plot_model(model, show_shapes=True, to_file='plots/model_plot_multichannel.png') return model
def build_model(train, n_input): # prepare data train_x, train_y = to_supervised(train, n_input, 365) # define parameters epochs, batch_size, verbose = 20, 16, 0 filters, kernel_size, pool_size = 64, 8, 4 dense_1, dense_2 = 500, 200 n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[ 2], train_y.shape[1] # create a channel for each variable in_layers, out_layers = list(), list() for i in range(n_features): inputs = Input(shape=(n_timesteps, 1)) conv1 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(inputs) conv2 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(conv1) conv3 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(conv2) conv4 = Conv1D(filters=filters, kernel_size=kernel_size, activation='relu')(conv3) pool1 = MaxPooling1D(pool_size=pool_size)(conv4) flat = Flatten()(pool1) # store layers in_layers.append(inputs) out_layers.append(flat) # merge heads merged = concatenate(out_layers) # interpretation by fully-connected layers dense1 = Dense(dense_1, activation='relu')(merged) dense2 = Dense(dense_2, activation='relu')(dense1) outputs = Dense(n_outputs)(dense2) model = Model(inputs=in_layers, outputs=outputs) # compile model model.compile(loss='mse', optimizer='adam') # fit network input_data = [ train_x[:, :, i].reshape((train_x.shape[0], n_timesteps, 1)) for i in range(n_features) ] model.fit(input_data, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose) # model.summary() # model.save('temp/CNN_multihead_model_1.h5') # approx. 55 MB per net # plot the model # plot_model(model, show_shapes=True, to_file='plots/model_plot_multihead.png') return model