def train(self, train_names, train_codes, hyperparams, pct_train=0.8, lr=0.01, num_epoch=100): if self.model_name == 'SimpleSeq2Seq' or self.model_name == 'AttentionSeq2Seq': train_name, train_code, val_name, val_code, naming_data, hyperparams['n_tokens'] = trainModel.split_data(train_names, train_codes, hyperparams['output_length'], hyperparams['input_length'], pct_train) hyperparams['is_embedding'] = False train_name = trainModel.one_hot_name(train_name, hyperparams['n_tokens']) required_params = ['output_dim', 'output_length', 'input_length', 'is_embedding', 'n_tokens'] for param in required_params: assert param in hyperparams, (param) if self.model_name == 'SimpleSeq2Seq': model = SimpleSeq2Seq(**hyperparams) elif self.model_name == 'AttentionSeq2Seq': model = AttentionSeq2Seq(**hyperparams) elif self.model_name == 'Seq2Seq': model = Seq2Seq(**hyperparams) else: raise TypeError my_adam = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08) model.compile(optimizer=my_adam, loss='categorical_crossentropy') print ('fit...') model.fit(train_code, train_name, epochs=num_epoch) print ('predict...') predict_probs = model.predict(val_code) predict_idx = np.argmax(predict_probs, axis=2) print('evaluate...') exact_match, _ = trainModel.exact_match(naming_data, predict_idx, val_name) precision, recall, f1, _, _ = trainModel.evaluate_tokens(naming_data, predict_idx, val_name) return model, exact_match, precision, recall, f1, naming_data
def train(self): model = Seq2Seq(batch_input_shape=(batch_size, n_steps_in, n_features_in), hidden_dim=200, output_length=n_steps_out, output_dim=n_features_out, depth=3) loss = tf.keras.losses.MeanSquaredError(name="Loss") metrics = [ tf.keras.metrics.MeanAbsolutePercentageError( name="mean_absolute_percentage"), tf.keras.metrics.MeanSquaredError(name="mean_square") ] checkpoint = ModelCheckpoint("model_save", monitor="mean_absolute_percentage", verbose=1, save_best_only=True, mode='min') model.compile(optimizer='adam', loss=loss, metrics=metrics) model.summary() model.fit(self.trainX, self.trainY, batch_size=batch_size, callbacks=[checkpoint], epochs=EPOCHS, validation_data=(self.testX, self.testY), shuffle=True)
def build_model(): model = Sequential() embedding_matrix = EmbeddingHolder().get_embeddings_mat() # BUILD ACTUAL MODEL embedding_layer = Embedding( 228999, EMBEDDING_DIM, weights=[embedding_matrix], input_length=INPUT_MAX_LENGTH, mask_zero=True ) s2s_layer = Seq2Seq( batch_input_shape=(None, INPUT_MAX_LENGTH, EMBEDDING_DIM), hidden_dim=HIDDEN_DIM, output_length=OUTPUT_MAX_LENGTH, output_dim=228999, depth=DEPTH ) # ADD UP ACTUAL MODEL model.add(embedding_layer) model.add(s2s_layer) model.compile(loss='mse', optimizer='adam', metrics=['accuracy', 'precision', 'recall']) print 'Model Built' return model
def build_model(self): self.model = Seq2Seq(input_length=self.input_length, input_dim=self.input_dim, hidden_dim=self.hidden_dim, output_length=self.output_length, output_dim=self.output_dim, depth=1)
def test_Seq2Seq(): x = np.random.random((samples, input_length, input_dim)) y = np.random.random((samples, output_length, output_dim)) models = [] models += [ Seq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim)) ] models += [ Seq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim), peek=True) ] models += [ Seq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim), depth=2) ] models += [ Seq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim), peek=True, depth=2) ] for model in models: model.compile(loss='mse', optimizer='sgd') model.fit(x, y, epochs=1) model = Seq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim), peek=True, depth=2, teacher_force=True) model.compile(loss='mse', optimizer='sgd') model.fit([x, y], y, epochs=1)
def build_model(self, **kwargs): return Seq2Seq(output_dim=self.feature_size if not kwargs else kwargs['output_size'], input_dim=self.feature_size, input_length=None, output_length=self.series_size, hidden_dim=self.hidden_dimensions, depth=self.depth, dropout=self.dropout)
def seq2seq_model(batch=1, seq_length=1): seq_model = Seq2Seq(input_shape=(seq_length, 128), hidden_dim=10, output_length=1, output_dim=128, depth=2, peek=True) seq_model.compile(loss='mse', optimizer='rmsprop') return seq_model
def build_seq2seq(time_step, input_dim, hidden_dim, depth=1, batch_size=None): model = Seq2Seq(batch_input_shape=(batch_size, time_step, input_dim), hidden_dim=hidden_dim, output_length=time_step, output_dim=input_dim, depth=depth) optimizer = RMSprop(lr=0.00001, rho=0.9, epsilon=1e-08, decay=0.0) model.compile(loss='mean_absolute_error', optimizer=optimizer) return model
def createModel(iLength=5, oLength=5, hidden=32, d=1, learning_rate=0.001): model = Seq2Seq(input_dim=156, input_length=iLength, hidden_dim=hidden, output_dim=156, output_length=oLength, depth=d) opt = optim.RMSprop(lr=learning_rate) model.compile(loss='mse', optimizer=opt) return model
def simple_s2s_lib(): model = Seq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_steps, input_shape=(input_steps, input_dim), peek=True, depth=(encoder_stack, decoder_stack), bidirectional=True) model.compile(loss='mse', optimizer='rmsprop') return model
def seq2seq_model(input_dim, hidden_dim, output_length, output_dim, input_length): model = Sequential() model_seq = Seq2Seq(input_dim=input_dim, hidden_dim=hidden_dim, output_length=output_length, output_dim=output_dim, input_length=input_length) model.add(model_seq) model.add(TimeDistributed(Dense(output_dim))) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') #model.compile(loss='categorical_crossentropy', optimizer='rmsprop') return model
def create_model(image_shape, max_caption_len, vocab_size): model = Sequential() model.add( Conv2D(32, (3, 3), input_shape=image_shape, kernel_initializer='he_normal', kernel_regularizer=l2(0.001))) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add( Conv2D(32, (3, 3), kernel_initializer='he_normal', kernel_regularizer=l2(0.001))) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add( Conv2D(64, (3, 3), kernel_initializer='he_normal', kernel_regularizer=l2(0.001))) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add( Conv2D(64, (3, 3), kernel_initializer='he_normal', kernel_regularizer=l2(0.001))) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(256, kernel_regularizer=l2(0.001))) model.add(LeakyReLU()) model.add(Dropout(0.4)) model.add(RepeatVector(1)) model.add( Seq2Seq(input_dim=256, input_length=1, hidden_dim=256, output_length=max_caption_len, output_dim=256, peek=True)) model.add(TimeDistributed(Dense(vocab_size))) model.add(Activation('softmax')) return model
def create_model(): # input = Input(shape=(BLOCK_LEN,)) # embedded = Embedding(input_dim=EVENTS_CNT + 1, input_length=BLOCK_LEN, output_dim=400)(input) # emb_model = Model(input, embedded) # print(emb_model.summary()) seq_model = Seq2Seq(batch_input_shape=(None, BLOCK_LEN, EVENTS_CNT), hidden_dim=56, output_length=BLOCK_LEN, output_dim=EVENTS_CNT, depth=1) # , teacher_force=True, unroll=True # print(seq_model.summary()) model = Sequential() # model.add(emb_model) model.add(seq_model) # model.add(Activation('softmax')) model.add(Dense(EVENTS_CNT, activation='softmax')) # model.summary() return model
def test(): x, y = LoadCorpus.load_chatbot100_train() # x, y = LoadCorpus.load_xiaohuangji_train() wv = LoadCorpus.load_wv60_model() def trans_seq(d): vector = [[wv[c] for c in s.split(' ') if c in wv.wv.vocab] for s in d] t = pad_sequences(vector, maxlen=max_words, padding='post', value=1., dtype='float32') return t def generate_decoder_input(decoder_output): word_dim = len(decoder_output[0][0]) word_start = np.zeros(shape=(word_dim, )) decoder_input = [] if not (decoder_input is decoder_output): for example in decoder_output: t = list(example[:14]) t.insert(0, word_start) decoder_input.append(np.array(t)) decoder_input = pad_sequences(decoder_input, maxlen=15, dtype='float32') return decoder_input train_x = trans_seq(x) train_y = trans_seq(y) y_input = generate_decoder_input(train_y) # model = SimpleSeq2Seq(input_dim=5, hidden_dim=10, output_length=8, output_dim=8, depth=3) model = Seq2Seq(batch_input_shape=(None, 15, 60), hidden_dim=256, output_length=15, output_dim=60, depth=3) model.compile(loss='mse', optimizer='rmsprop', metrics=['accuracy']) print(model.summary()) model.fit(train_x, y_input, epochs=epochs, batch_size=8)
def compile_model(input_length, input_depth, input_dim, hidden_dim, output_length, output_depth, output_dim, peek, attention, loss, optimizer): """ Returns a compiled seq2seq model Arguments: input_length - Batch size in which to partition the elements input_depth - the number of layers in encoder input_dim - the number of features for each word hidden_dim - number of hidden units output_length - (= maximum_output_length) The maximum number of words in output output_depth - the number of layers in decoder output_dim - the number of features in word embedding's output peek - (binray) add the peek feature attention - (binary) use attention model loss - (string) the loss function, one of keras options optimizer - (string) the optimizer function, one of keras options """ # Only pass peek as an argument if using an attention model model_fn = lambda **args: \ AttentionSeq2Seq(**args) \ if attention \ else Seq2Seq(peek = peek, **args) model = model_fn( input_length=input_length, input_dim=input_dim, hidden_dim=hidden_dim, output_length=output_length, output_dim=output_dim, depth=(input_depth, output_depth), ) model.compile(loss=loss, optimizer=optimizer) if logging.getLogger().getEffectiveLevel() == logging.DEBUG: model.summary() return model
def create_model(X_vocab_len, X_max_len, y_vocab_len, y_max_len, hidden_dim, layer_num, learning_rate, dropout): model = Sequential() model.add(Seq2Seq(output_dim=y_vocab_len, hidden_dim=hidden_dim, output_length=y_max_len, input_shape=(X_max_len, X_vocab_len), depth=layer_num, dropout=dropout)) model.add(TimeDistributed(Dense(y_vocab_len, activation='softmax'))) model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=learning_rate), metrics=['accuracy']) # Creating encoder network # model.add(LSTM(hidden_dim, input_shape=(X_max_len, X_vocab_len), dropout=dropout, recurrent_dropout=dropout)) # model.add(RepeatVector(y_max_len)) # Creating decoder network # for _ in range(layer_num): # model.add(LSTM(hidden_dim, return_sequences=True, dropout=dropout, recurrent_dropout=dropout)) # model.add(TimeDistributed(Dense(y_vocab_len, activation='softmax'))) # model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=learning_rate), metrics=['accuracy']) return model
def create_imgText(image_shape, max_caption_len, vocab_size): image_model = Sequential() # image_shape : C,W,H # input: 100x100 images with 3 channels -> (3, 100, 100) tensors. # this applies 32 convolution filters of size 3x3 each. image_model.add( Convolution2D(32, 3, 3, border_mode='valid', input_shape=image_shape)) image_model.add(BatchNormalization()) image_model.add(Activation('relu')) image_model.add(Convolution2D(32, 3, 3)) image_model.add(BatchNormalization()) image_model.add(Activation('relu')) image_model.add(MaxPooling2D(pool_size=(2, 2))) image_model.add(Dropout(0.25)) image_model.add(Convolution2D(64, 3, 3, border_mode='valid')) image_model.add(BatchNormalization()) image_model.add(Activation('relu')) image_model.add(Convolution2D(64, 3, 3)) image_model.add(BatchNormalization()) image_model.add(Activation('relu')) image_model.add(MaxPooling2D(pool_size=(2, 2))) image_model.add(Dropout(0.25)) image_model.add(Flatten()) # Note: Keras does automatic shape inference. image_model.add(Dense(128)) image_model.add(RepeatVector(1)) #model = AttentionSeq2Seq(input_dim=128, input_length=1, hidden_dim=128, output_length=max_caption_len, output_dim=vocab_size) model = Seq2Seq(input_dim=128, input_length=1, hidden_dim=128, output_length=max_caption_len, output_dim=128, peek=True) image_model.add(model) image_model.add(TimeDistributed(Dense(vocab_size))) image_model.add(Activation('softmax')) return image_model
def build_seq2seq_model(): """ Builds and returns the model based on the global config, but using the seq2seq library: https://github.com/farizrahman4u/seq2seq """ import seq2seq from seq2seq.models import Seq2Seq model = Seq2Seq( input_dim=N_FEATURES, input_length=MAX_EQUATION_LENGTH, hidden_dim=HIDDEN_SIZE, output_length=MAX_RESULT_LENGTH, output_dim=N_FEATURES, depth=(ENCODER_DEPTH, DECODER_DEPTH), ) model.compile( loss='mse', optimizer='adam', metrics=['accuracy'], ) return model
def model(self,batch_input_shape,hidden_dim,output_length,output_dim,depth): #model = Seq2Seq(batch_input_shape=(16, 7, 5), hidden_dim=10, output_length=8, output_dim=20, depth=4) self.model = Seq2Seq(batch_input_shape=batch_input_shape, hidden_dim=hidden_dim, output_length=output_length, output_dim=output_dim, depth=depth) self.model.compile(loss='mse', optimizer='rmsprop')
nTrainRound = (nTrain // batchSize) * batchSize nVal = Xval.shape[0] nValRound = (nVal // batchSize) * batchSize nTest = Xtest.shape[0] nTestRound = (nTest // batchSize) * batchSize Xtrain = Xtrain[:nTrainRound, :, :] ytrain = ytrain[:nTrainRound, :, :] Xval = Xval[:nValRound, :, :] yval = yval[:nValRound, :, :] Xtest = Xtest[:nTestRound, :, :] ytest = ytest[:nTestRound, :, :] model = Seq2Seq(batch_input_shape=(batchSize, 803, 4), hidden_dim=100, output_length=803, output_dim=1, depth=2) model.compile(loss='mse', optimizer='adam') model.fit(Xtrain, ytrain, batch_size=batchSize, nb_epoch=3, validation_data=(Xval, yval)) yval_preds = model.predict(Xval, batchSize, verbose=1) def saveFigs(): for i in range(3): plt.figure()
def get_model(story_maxlen, seq_maxlen, num_inputs, answer_maxlen, char_embedding_size=30, hidden_dim=150, encoder_depth=1, decoder_depth=1, conv_nfilters=[], query_lstm_dims=[], hierarchical_lstm_dims=[], query_maxlen=None, dropout=0.5, seq2seq_model="attention", hierarchical=False): assert seq2seq_model in ["attention", "simple", "seq2seq"] if hierarchical: assert story_maxlen is not None, "Need story maxlen for hierarchical model" input_shape = (story_maxlen, seq_maxlen) else: input_shape = (seq_maxlen, ) # TODO Need to manually add this for correct output dims? Probably not since we add our own dense layer on top # We WOULD need this if we eliminate last dense layer and have only an activation there. # hidden_dims_decoder.append(num_inputs) input1 = Input(shape=input_shape) inputs = [input1] embed_inner_input = Input((input_shape[-1], )) embed1 = Embedding(num_inputs, char_embedding_size)(embed_inner_input) embed1 = Dropout(dropout)(embed1) pool = embed1 for conv_nfilter in conv_nfilters: conv_len3 = Convolution1D(conv_nfilter, 3, activation="tanh", border_mode="same")(pool) conv_len3 = Dropout(dropout)(conv_len3) conv_len2 = Convolution1D(conv_nfilter, 2, activation="tanh", border_mode="same")(pool) conv_len2 = Dropout(dropout)(conv_len2) pool1 = MaxPooling1D(pool_length=2, stride=2, border_mode='valid')(conv_len3) pool2 = MaxPooling1D(pool_length=2, stride=2, border_mode='valid')(conv_len2) # could try merge with sum here instead, and no dense layer pool = concatenate([pool1, pool2]) pool = TimeDistributed(Dense(conv_nfilter, activation="tanh"))(pool) if hierarchical: ln.debug("Inner LSTM input len is %s" % (pool._keras_shape[1])) seq_embedding = pool for hierarchical_lstm_dim in hierarchical_lstm_dims[:-1]: seq_embedding = LSTM(hierarchical_lstm_dim, return_sequences=True)(seq_embedding) seq_embedding = LSTM(hierarchical_lstm_dims[-1], return_sequences=False)(seq_embedding) embed_inner_model = Model(embed_inner_input, seq_embedding) seqs_embedded = TimeDistributed(embed_inner_model)(input1) else: embed_inner_model = Model(embed_inner_input, pool) seqs_embedded = embed_inner_model(input1) ln.debug("Will attend over %s time steps" % seqs_embedded._keras_shape[1]) if query_maxlen is not None: input2 = Input(shape=(query_maxlen, )) inputs.append(input2) embed2 = Embedding(num_inputs, char_embedding_size)(input2) # conv_embed2 = Convolution1D(hidden_dim, 2, activation="relu", border_mode="same")(embed2) # pool2 = MaxPooling1D(pool_length=2, border_mode='valid')(conv_embed2) query_encoded = embed2 for query_lstm_dim in query_lstm_dims[:-1]: query_encoded = LSTM(query_lstm_dim, return_sequences=True)(query_encoded) query_encoded = LSTM(query_lstm_dims[-1], return_sequences=False)(query_encoded) query_encoded = RepeatVector( seqs_embedded._keras_shape[1])(query_encoded) seqs_embedded = concatenate([seqs_embedded, query_encoded]) if seq2seq_model == "attention": decoded = AttentionSeq2Seq( batch_input_shape=seqs_embedded._keras_shape, hidden_dim=hidden_dim, output_dim=hidden_dim, depth=(encoder_depth, decoder_depth), output_length=answer_maxlen, # TODO add dropout once it works )(seqs_embedded) elif seq2seq_model == "seq2seq": decoded = Seq2Seq(batch_input_shape=seqs_embedded._keras_shape, hidden_dim=hidden_dim, output_dim=hidden_dim, depth=(encoder_depth, decoder_depth), output_length=answer_maxlen, peek=True)(seqs_embedded) else: decoded = SimpleSeq2Seq(batch_input_shape=seqs_embedded._keras_shape, hidden_dim=hidden_dim, output_dim=hidden_dim, depth=(encoder_depth, decoder_depth), output_length=answer_maxlen, dropout=dropout)(seqs_embedded) pred = TimeDistributed(Dense(num_inputs, activation="softmax"))(decoded) model = Model(inputs=inputs, outputs=pred) return model
from seq2seq.data_loader import load_seq2seq_data from seq2seq.models import Seq2Seq, Encoder, Decoder from seq2seq.trainer import Seq2SeqTrainer corpus = load_seq2seq_data('./data/sample.txt') src_dict = corpus.make_vocab_dictionary('src') trg_dict = corpus.make_vocab_dictionary('trg') print('Data Loaded...') encoder = Encoder(len(src_dict.idx2item), 256, 512, 2, 0.5) decoder = Decoder(len(trg_dict.idx2item), 256, 512, 2, 0.5) seq2seq = Seq2Seq(encoder, decoder, src_dict, trg_dict) print('Model initialized...') trainer = Seq2SeqTrainer(seq2seq, corpus) print('Begin to train...') trainer.train('test')
# Use each item of the paraphrase pairs when training the autoencoder # The target size is (n_training, max_sentence_length, embedding_size) print "Reshaping training data" X_train = np.reshape(train_inputs, (-1, MAX_SENTENCE_LENGTH, GLOVE_SIZE)) X_test = np.reshape(test_inputs, (-1, MAX_SENTENCE_LENGTH, GLOVE_SIZE)) # Build the Siamese network callback siamese = ParaphraseCallback(train_inputs, train_labels, test_inputs, test_labels) # Train the autoencoder print "Building Seq2Seq model" autoencoder = Seq2Seq(input_dim=GLOVE_SIZE, hidden_dim=HIDDEN_SIZE, output_length=MAX_SENTENCE_LENGTH, output_dim=GLOVE_SIZE, depth=HIDDEN_DEPTH) print "Compiling Seq2Seq model" autoencoder.compile(loss='mse', optimizer='rmsprop') print "Fitting Seq2Seq model" autoencoder.fit([X_train, X_train], [X_train, X_train], nb_epoch=10, batch_size=128, callbacks=[siamese]) print "Evaluating on training set" autoencoder.evaluate([X_train, X_train], [X_train, X_train], batch_size=128)
print i, "/", total_dialogs X.append(x_sequence) Y.append(y_sequence) X = np.array(X) Y = np.array(Y) num_ex, sequence_length, vec_size = X.shape print X.shape word_model = gensim.models.Word2Vec.load("data/generated/words_vector.gensim") model = Seq2Seq(input_dim=vec_size, hidden_dim=vec_size, output_length=sequence_length, output_dim=vec_size, depth=2, dropout=0.3) model.load_weights("data/generated/2018-05-07 02:01:09/iter_51.h5") print "Testing.." while True: msg = raw_input().decode("utf-8") vector_input = message2vector(word_model, sequence_length - 2, msg) vector_output = model.predict(np.array([vector_input]))[0] words_output = [ word_model.similar_by_vector(vector_word)[0][0] for vector_word in vector_output ]
from visualization import * #endregion sequence_length = K n_features = F model = Sequential() ################### ### Model Definition ################### if(seqToSeq): if(False): model = Seq2Seq(input_shape = (sequence_length, n_features), hidden_dim = 25, output_length = sequence_length, output_dim = n_features, depth = 1, peek = True # where the decoder gets a 'peek' at the context vector at every timestep ) model.compile(#loss = 'mse' , optimizer = 'rmsprop'#metrics=[TE] ,loss=Mortality_Seq2seq_Metric_Loss , metrics=['mean_squared_error']) if(True): #mehrdad sequence_length = K n_features = F model = Sequential() model.add(LSTM(K, input_shape=(sequence_length, n_features))) model.add(RepeatVector(sequence_length)) model.add(LSTM(K, return_sequences=True)) model.add(TimeDistributed(Dense(n_features #,activation='sigmoid'
X_train = X[:train_idx] y_train = y[:train_idx] X_test = X[train_idx:] y_test = y[train_idx:] print("[MESSAGE] Vectorization completed") print("[MESSAGE] Build Model...") BATCH_SIZE = 128 model = Sequential() model.add( Seq2Seq(input_dim=input_dim, input_length=MAX_CHAR, hidden_dim=100, output_length=MAX_CHAR, output_dim=output_dim, depth=4)) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=["accuracy"]) model.summary() print("[MESSAGE] The model is built.") early_stopping = EarlyStopping(monitor='val_loss', patience=3) model_name = os.path.join(e2p.E2P_PATH, "trans_test") model_name += "-{epoch:02d}-{val_acc:.2f}.hdf5"
def ha_mfcc(): ### load mfcc input wavpath = '/data/home/troutman/lab/timit/train/wav/' cfgpath = '/data/home/troutman/lab/STD/Pattern/zrst/matlab/hcopy.cfg' word_align_path = '/data/home/troutman/lab/timit/train/word_all.txt' weight_dir = 'model_weights' weight_name = 'ae_mfcc.h' ### check save weight file exist or not if not os.path.exists(weight_dir): os.makedirs(weight_dir) if os.path.isfile(join(weight_dir, weight_name)): stdin = raw_input( 'overwrite existed file {} ?(y,n)'.format(weight_name)) if 'n' in stdin: sys.exit(1) mfcc_feats = [] # shape:(nb_wavfiles,nb_frame,39) wavfiles = [ join(wavpath, f) for f in listdir(wavpath) if isfile(join(wavpath, f)) ] ### load mfcc feats wavfiles, mfcc_feats = load_mfcc_train(wavfiles, cfgpath) ### load word_align word_aligns = {} load_word_align(word_align_path, word_aligns) ### build model model = Seq2Seq(input_shape=(fix_len, 39), output_dim=39, output_length=fix_len, hidden_dim=hid_dim) model.compile(loss='mse', optimizer='rmsprop', sample_weight_mode="temporal") model.summary() encoder_weights = [] decoder_weights = [ ] # store layer[-2], layer[-1] weights for each target decoder decoder_weights.append( [model.layers[-2].get_weights(), model.layers[-1].get_weights()]) encoder_weights = [ model.layers[0].get_weights(), model.layers[1].get_weights(), model.layers[2].get_weights() ] #nb_batch = int(math.ceil(len(wavfiles)/float(batch_size))) for epoch in range(nb_epoch): print 'Epoch: {:3d}'.format(epoch) start_time = time.time() epoch_loss = 0. ### for each target for bs in range(1, len(buckets)): print ' bucket: {}'.format(buckets[bs]) bs_start_time = time.time() ### shuffle data wavfiles, mfcc_feats = shuffle_mfcc_input(wavfiles, mfcc_feats) model = MySeq2Seq(input_shape=(buckets[bs], 39), output_dim=39, output_length=buckets[bs], hidden_dim=hid_dim) model.compile(loss='mse', optimizer='rmsprop', sample_weight_mode="temporal") seq2seq_load_weights(model, encoder_weights, decoder_weights[0]) history = model.fit_generator(Mygenerator_mfcc(wavfiles,mfcc_feats,word_aligns,(buckets[bs-1],buckets[bs])),\ samples_per_epoch=buckets_sample[bs-1], nb_epoch=1) seq2seq_save_weights(model, encoder_weights, decoder_weights[0]) target_loss = history.history['loss'][-1] epoch_loss += target_loss * buckets_sample[bs - 1] / sum(buckets_sample) print (' Epoch {:3d} loss: {:.5f} fininish in: {:.5f} sec'.\ format(epoch,target_loss,time.time() - bs_start_time)) print('Epoch {:3d} loss: {:.5f} fininish in: {:.5f} sec'.format( epoch, epoch_loss, time.time() - start_time)) model.save_weights(join(weight_dir, weight_name))
print(y_val.shape) # -- # %% from keras.models import Sequential from keras.layers import LSTM, RepeatVector, Dense, Activation, TimeDistributed from seq2seq.models import SimpleSeq2Seq, AttentionSeq2Seq, Seq2Seq BATCH_SIZE = 64 print('Build model...') model = Sequential() # model.add(AttentionSeq2Seq(32, 4, input_dim=len(chars), input_length=MAXLEN, hidden_dim=64)) model.add( Seq2Seq(32, 4, input_dim=len(chars), input_length=MAXLEN, hidden_dim=64)) # model.add(SimpleSeq2Seq(32, 4, input_dim=len(chars), input_length=MAXLEN, hidden_dim=64)) model.add(Dense(12)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() # %% history = model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=10, validation_data=(x_val, y_val))
def model(self): self.model = Seq2Seq(batch_input_shape=(16, 7, 5), hidden_dim=10, output_length=8, output_dim=20, depth=4, peek=True) self.model.compile(loss='mse', optimizer='rmsprop')