def fitting(self, model: Sequential) -> Sequential:
     # 予測値はロジットや対数オッズ比で出力される
     predictions = model(self.data_source.x_train[:1]).numpy()
     # 確率に変換
     probability = tf.nn.softmax(predictions).numpy()
     # 損失関数。下記の書き方をすればそれぞれの標本についてクラスごとに損失のスカラを返す
     loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(
         from_logits=True)
     # loss確認する場合はコメント外す
     # loss = loss_fn(mnist.y_train[:1], predictions).numpy()
     model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
     model.fit(self.data_source.x_train, self.data_source.y_train, epochs=5)
     model.evaluate(self.data_source.x_test,
                    self.data_source.y_test,
                    verbose=2)
     return model
Beispiel #2
0
class Model:
    __batch_size = 100

    def __init__(self):
        self.model = Sequential([
            Dense(40, input_shape=(4, ), activation="relu"),
            Dense(40, activation="relu"),
            Dense(40, activation="relu"),
            Dense(4, activation="tanh")
        ])
        # TODO: xavier initialization?
        self.model.compile(loss=keras.losses.mean_squared_error,
                           optimizer=keras.optimizers.Adam(lr=0.001))

        self.memory = deque(maxlen=10000)

    def experience_replay(self):
        if len(self.memory) < self.__batch_size:
            return

        batch = random.sample(self.memory, self.__batch_size)
        states = np.vstack([state for state, _, _, _, _ in batch])
        next_states = np.vstack(
            [next_state for _, _, _, next_state, _ in batch])

        predicted_states = self.model.predict(states)
        predicted_next_states = self.model.predict(next_states)
        max_nex_state_values = np.max(predicted_next_states, 1)

        for index, (_, action, reward, _, terminal) in enumerate(batch):
            q_update = reward

            if not terminal:
                discount_factor = 0.95
                q_update += discount_factor * max_nex_state_values[index]

            learning_rate = 0.95
            predicted_states[index][action] = (
                (1 - learning_rate) * predicted_states[index][action] +
                learning_rate * q_update)
        self.model.fit(states, predicted_states, verbose=0)
Beispiel #3
0
largest = 10000
alphabet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '+', ' ']
n_chars = len(alphabet)
n_in_seq_length = n_numbers * ceil(log10(largest + 1)) + n_numbers - 1
n_out_seq_length = ceil(log10(n_numbers * (largest + 1)))
n_batch = 100
n_epoch = 500
model = Sequential([
    LSTM(100, input_shape=(n_in_seq_length, n_chars)),
    RepeatVector(n_out_seq_length),
    LSTM(50, return_sequences=True),
    TimeDistributed(Dense(n_chars, activation='softmax'))
])

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
for i in range(n_epoch):
    x, y = generate_data(n_samples, largest, alphabet)
    model.fit(x, y, epochs=1, batch_size=n_batch)

model.save('training/keras_classifier.h5')

# evaluate on some new patterns
x, y = generate_data(n_samples, largest, alphabet)
result = model.predict(x, batch_size=n_batch, verbose=0)
# calculate error
expected = [invert(x, alphabet) for x in y]
predicted = [invert(x, alphabet) for x in result]
# show some examples
for i in range(20):
print('단어 카운트:', token.word_counts)
print('문장 카운트:', token.document_count)
print('각 단어가 몇개의 문장에 포함되어 있는가 :', token.word_docs)
print('각 단어에 매겨진 인덱스 값 :', token.word_index)

print()
# 텍스트를 읽고 긍정 , 부정 분류 예측 

docs = ['너무 재밌네요', '최고에요','참 잘만든 영화예요','추천하고 싶은 영화네요','한번 더 보고싶네요',
        '글쎄요','별로네요','생각보다 지루합니다','연기가 좋지않아요','재미없어요']

import numpy as np 
classes = np.array([1,1,1,1,1,0,0,0,0,0])

token = Tokenizer()
token.fit_on_texts(docs)
print(token.word_index)

model = Sequential()
model.add(Embedding(word_size,8,input_length=4))
#model.add(Flatten())
model.add(LSTM(32))
model.add(Dense(1,activation='sigmoid'))

print(model.summary())
model.compile(optimizer='adam',loss='binary_crossentropy')




Beispiel #5
0
        ),  #Replaces negative values with zero and keeps positive values
        Dense(512),
        Activation('relu'),
        Dense(10),
        Activation('softmax')  #Normalisation
    ])

    #Could change this optimiser
    #Adam optimisation is a stochastic gradient descent method
    #Can handle sparse gradients on noisy problems
    optimiser = keras.optimizers.Adam(learning_rate=0.01)

    #Training configuration
    model.compile(
        loss='categorical_crossentropy',  #Loss function for one hot inputs
        optimizer=optimiser,
        metrics=['accuracy'],
    )

    #Train the model
    print("Fit model on training data")
    history = model.fit(x_train,
                        y_train,
                        validation_split=0.2,
                        batch_size=64,
                        epochs=20)  #initially 15

    #Visualise model
    print("Model history keys")
    print(history.history.keys())
Beispiel #6
0
def build_model(use_gpu: bool = False,
                num_units: int = 64,
                num_layers: int = 1,
                dropout_rate: float = 0.0,
                batch_size: int = 1000,
                window_size: int = 10,
                num_params: int = 0):
    """
    Builds the RNN-Model for character prediction.

    :param window_size: Sequence size
    :param batch_size: {int} Size of batch
    :param dropout_rate: {float} Regulating Dropout rate between layers
    :param num_layers: {int} Number of layers to build
    :param num_units: {int} Number of LSTM-Units to use in network
    :param use_gpu: {bool} Uses Tensorflow GPU support if True, otherwise trains on CPU
    :param num_params: {int} Number of control parameters
    :return: Keras model
    """

    # Load max 5000 entries from the dataset to build the Tokenizer / vocabulary
    loader = Loader(min(batch_size, 5000), 0)
    tokenizer = Tokenizer(filters='', split='°', lower=False)

    for dataframe in loader:

        chars = set()

        for name in dataframe['name']:
            chars.update(set(str(name)))

        tokenizer.fit_on_texts(list(chars))

    tokenizer.fit_on_texts(['pre', '<end>', 'pad'])

    # Build Keras Model
    model = Sequential()
    for r in range(0, max(num_layers - 1, 0)):
        model.add(layer=(CuDNNLSTM if use_gpu else LSTM
                         )(num_units,
                           input_shape=(window_size,
                                        len(tokenizer.index_word) + 1 +
                                        num_params),
                           return_sequences=True))
        model.add(Dropout(dropout_rate))

    model.add(
        layer=(CuDNNLSTM if use_gpu else LSTM)(num_units,
                                               input_shape=(
                                                   window_size,
                                                   len(tokenizer.index_word) +
                                                   1 + num_params)))
    model.add(Dense(len(tokenizer.index_word) + 1, activation='softmax'))

    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    # Show summary
    print(model.summary())

    return model, tokenizer
def build_model():
    """
        Function that build the CNN + LSTM network
    """
    with tf.name_scope('CNN_LSTM'):
        model = Sequential()

        with tf.name_scope('Conv1'):
            model.add(
                TimeDistributed(Convolution2D(16, (5, 5),
                                              padding='same',
                                              strides=(2, 2)),
                                input_shape=(15, 16, 3200, 1),
                                name='Conv1'))

        model.add(BatchNormalization())
        model.add(Activation('relu'))

        with tf.name_scope('Conv2'):
            model.add(
                TimeDistributed(
                    Convolution2D(32, (5, 5),
                                  padding='same',
                                  strides=(1, 1),
                                  name='Conv2')))
            model.add(Activation('relu'))

        with tf.name_scope('Pooling'):
            model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))

        with tf.name_scope('Conv3'):
            model.add(
                TimeDistributed(
                    Convolution2D(32, (5, 5),
                                  padding='same',
                                  strides=(1, 1),
                                  name='Conv3')))
            model.add(Activation('relu'))

        with tf.name_scope('Conv4'):
            model.add(
                TimeDistributed(
                    Convolution2D(32, (5, 5),
                                  padding='same',
                                  strides=(1, 1),
                                  name='Conv4')))
            model.add(Activation('relu'))

        with tf.name_scope('Pooling'):
            model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))

        with tf.name_scope('FC1'):
            model.add(TimeDistributed(Flatten(), name='FC1'))
            model.add(Activation('relu'))

            model.add(TimeDistributed(Dropout(0.25)))

        with tf.name_scope('FC2'):
            model.add(TimeDistributed(Dense(256), name='FC2'))
            model.add(Activation('relu'))

            model.add(TimeDistributed(Dropout(0.25)))

        with tf.name_scope('LSTM'):
            model.add(tf.keras.layers.CuDNNLSTM(64, return_sequences=False))
            model.add(Dropout(0.5))

        with tf.name_scope('OutputLayer'):
            model.add(Dense(2, activation='softmax'))

    with tf.name_scope('Optimizer'):
        optimizer = optimizers.adam(lr=1e-4, decay=1e-5)

    with tf.name_scope('Loss'):
        model.compile(loss='categorical_crossentropy',
                      optimizer=optimizer,
                      metrics=['accuracy'])

    return model
Beispiel #8
0
epoch = 50
batch = 100
num_chars = converter.char_len()
largest = converter.max_value()

n_out_seq_length = ceil(log10(numbers * (largest + 1)))

model = Sequential([
    LSTM(100, input_shape=(converter.max_number_length, num_chars)),
    RepeatVector(n_out_seq_length),
    LSTM(50, return_sequences=True),
    TimeDistributed(Dense(1, activation='softmax'))
])

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
for i in range(epoch):
    x, y = generator.build_sample(samples, largest)
    print(converter.max_number_length, num_chars)
    print(len(x[0]), len(x[0][0]))
    model.fit(x, y, epochs=1, batch_size=batch)

model.save('training/classifier.h5')

# evaluate on some new patterns
x, y = generator.build_sample(samples, largest)
result = model.predict(x, batch_size=batch, verbose=0)

# calculate error
expected = [converter.invert(x) for x in y]