示例#1
0
def test_SimpleSeq2Seq():
    x = np.random.random((samples, input_length, input_dim))
    y = np.random.random((samples, output_length, output_dim))

    models = []
    models += [
        SimpleSeq2Seq(output_dim=output_dim,
                      output_length=output_length,
                      input_shape=(input_length, input_dim))
    ]
    models += [
        SimpleSeq2Seq(output_dim=output_dim,
                      output_length=output_length,
                      input_shape=(input_length, input_dim),
                      depth=2)
    ]

    for model in models:
        model.compile(loss='mse', optimizer='sgd')
        model.fit(x, y, nb_epoch=1)
示例#2
0
文件: test.py 项目: nobodywy/seq2seq
def test_SimpleSeq2Seq():
    x = np.random.random((samples, input_length, input_dim))
    y = np.random.random((samples, output_length, output_dim))

    models = []
    models += [SimpleSeq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim))]
    #models += [SimpleSeq2Seq(output_dim=output_dim, hidden_dim=hidden_dim, output_length=output_length, input_shape=(input_length, input_dim), depth=2)]

    for model in models:
        model.compile(loss='mse', optimizer='sgd')
        plot_model(model, to_file='model3.png', show_shapes=True)
示例#3
0
def test_SimpleSeq2Seq():
    x = np.random.random((batch, max_encoder_length, input_dim))
    y = np.random.random((batch, max_decoder_length, output_dim))

    models = []
    models += [
        SimpleSeq2Seq(output_dim=output_dim,
                      hidden_dim=hidden_dim,
                      output_length=max_decoder_length,
                      input_shape=(max_encoder_length, input_dim))
    ]
    models += [
        SimpleSeq2Seq(output_dim=output_dim,
                      hidden_dim=hidden_dim,
                      output_length=max_decoder_length,
                      input_shape=(max_encoder_length, input_dim),
                      depth=2)
    ]

    for model in models:
        model.compile(loss='mse', optimizer='sgd')
        model.fit(x, y, nb_epoch=1)
示例#4
0
文件: s2s.py 项目: mbencherif/Neural
  # turn x and y into numpy array among other things
  train_x = pad_sequences(train_x, maxlen=maxlen_x)
  train_y = pad_sequences(train_y, maxlen=maxlen_y)
  print train_y.shape
  print train_y

  # convert train_y into (num_examples, maxlen, alphabet_size)
  # train_y = to_categorical(np.array(train_y), classes)

  model = Sequential()
  model.add(Embedding(input_dim=len(dataset.input2int),
                      output_dim=cfg.getint('cnn', 'embdims'),
                      input_length=maxlen_x))
  #model.add(SimpleSeq2Seq(input_dim=5, hidden_dim=10,
  #                        output_length=8, output_dim=8))
  model.add(SimpleSeq2Seq(hidden_dim=10, output_length=maxlen_y, output_dim=len(dataset.output2int)))
  model.compile(loss='mse', optimizer='rmsprop')

  model.fit(train_x,
            train_y,
            epochs=cfg.getint('cnn', 'epochs'),
            batch_size=cfg.getint('cnn', 'batch'),
            verbose=1,
            validation_split=0.0)

  # get_weights() returns a list of 1 element
  # dump these weights to file (word2vec model format)
  weights = model.layers[0].get_weights()[0]
  word2vec.write_vectors(dataset.word2int, weights, 'weights.txt')

  # probability for each class; (test size, num of classes)
示例#5
0
    partial_train_data = np.concatenate([
        source_data[:i * num_val_samples],
        source_data[(i + 1) * num_val_samples:]
    ],
                                        axis=0)
    partial_train_targets = np.concatenate([
        target_data[:i * num_val_samples],
        target_data[(i + 1) * num_val_samples:]
    ],
                                           axis=0)

    # Build the Keras model (already compiled)
    model = SimpleSeq2Seq(input_dim=9,
                          input_length=3,
                          hidden_dim=10,
                          output_length=1,
                          output_dim=1,
                          dropout=0.3,
                          depth=3)
    # model = Seq2Seq(input_dim=9, input_length=3,hidden_dim=10, output_length=1, output_dim=1, depth=4)
    # model = AttentionSeq2Seq(input_dim=9, input_length=3, hidden_dim=10, output_length=1, output_dim=1, depth=3)
    model.compile(loss='mse', optimizer='adam')

    # Train the model (in silent mode, verbose=0)
    model.fit(partial_train_data,
              partial_train_targets,
              epochs=num_epochs,
              batch_size=128,
              verbose=1)

    # Evaluate the model on the validation data