class NNQ: def __init__(self, input_space, action_space): #HyperParameters self.GAMMA = 0.95 self.LEARNING_RATE = 0.002 self.MEMORY_SIZE = 1000000 self.BATCH_SIZE = 30 self.EXPLORATION_MAX = 1.0 self.EXPLORATION_MIN = 0.01 self.EXPLORATION_DECAY = 0.997 self.exploration_rate = self.EXPLORATION_MAX self.reward = 0 self.actions = action_space #Experience Replay self.memory = deque(maxlen=self.MEMORY_SIZE) #Create the NN model self.model = Sequential() self.model.add( Dense(64, input_shape=(input_space, ), activation="relu")) self.model.add(Dense(64, activation="relu")) self.model.add(Dense(self.actions, activation="softmax")) self.model.compile(loss="mse", optimizer=Adam(lr=self.LEARNING_RATE)) def act(self, state): #Exploration vs Exploitation if np.random.rand() < self.exploration_rate: return random.randrange(self.actions) q_values = self.model.predict(state) return np.argmax(q_values[0]) def remember(self, state, action, reward, next_state, done): #in every action put in the memory self.memory.append((state, action, reward, next_state, done)) def experience_replay(self): #When the memory is filled up take a batch and train the network if len(self.memory) < self.MEMORY_SIZE: return batch = random.sample(self.memory, self.BATCH_SIZE) for state, action, reward, next_state, terminal in batch: q_update = reward if not terminal: q_update = ( reward + self.GAMMA * np.amax(self.model.predict(next_state)[0])) q_values = self.model.predict(state) q_values[0][action] = q_update self.model.fit(state, q_values, verbose=0) if self.exploration_rate > self.EXPLORATION_MIN: self.exploration_rate *= self.EXPLORATION_DECAY
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')
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
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 seq_lstm(self, y_cols_idx=[-1]): from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense, LSTM, Dropout len_outputs = len(self.y_cols) len_features = len(self.x_cols) regressior = Sequential() regressior.add( LSTM( units=60, activation="relu", return_sequences=True, input_shape=(self.x_train.shape[1], len_features), ) ) regressior.add(Dropout(0.2)) regressior.add(LSTM(units=120, activation="relu", return_sequences=True)) regressior.add(Dropout(0.2)) regressior.add(LSTM(units=240, activation="relu", return_sequences=True)) regressior.add(Dropout(0.2)) regressior.add(LSTM(units=240, activation="relu", return_sequences=True)) regressior.add(Dropout(0.2)) regressior.add(LSTM(units=120, activation="tanh")) regressior.add(Dropout(0.2)) regressior.add(Dense(units=len_outputs)) self.model = regressior # self.save("my_model") # print(self.model.summary()) # print("model done") return regressior pass