def _build_network(self, dim_a, params): with tf.variable_scope('base'): # Initialize graph if params['train_ae']: ae_trainer = TrainAE() ae_trainer.run(train=True, show_performance=True) self.AE = AE(ae_loc=params['ae_loc']) ae_encoder = self.AE.latent_space self.low_dim_input_shape = ae_encoder.get_shape()[1:] self.low_dim_input = tf.placeholder(tf.float32, [ None, self.low_dim_input_shape[0], self.low_dim_input_shape[1], self.low_dim_input_shape[2] ], name='input') self.low_dim_input = tf.identity(self.low_dim_input, name='low_dim_input') # Build fully connected layers self.y, loss = fully_connected_layers( tf.contrib.layers.flatten(self.low_dim_input), dim_a, params['fc_layers_neurons'], params['loss_function_type']) variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'base') self.train_policy = tf.train.GradientDescentOptimizer( learning_rate=params['learning_rate']).minimize(loss, var_list=variables) # Initialize tensorflow init = tf.global_variables_initializer() self.sess = tf.Session() self.sess.run(init) self.saver = tf.train.Saver()
def setUp(self): # construction of Encoder_Decoder object hidden = [128, 2] act_func = 'tanh' out_func = 'sigmoid' use_BN = True init_method = nn.init.xavier_uniform_ is_gauss_dist = 'True' device = 'cuda' self.ed = Encoder_Decoder(hidden, act_func, out_func, use_BN, init_method, is_gauss_dist=is_gauss_dist, device=device) # construction of AutoEncoder object input_shape = 784 fd = './model/torch' self.ae = AE(input_shape ,hidden, act_func=act_func, out_func=out_func, use_BN=use_BN, folder=fd, is_gauss_dist=is_gauss_dist, device=device) # torchのMNISTの呼び出し方がよくわからんかったのでchainerで代用 # MNISTデータの読み込み import chainer self.train, self.test = chainer.datasets.get_mnist() # データとラベルに分割 self.train_data, self.train_label = self.train._datasets self.test_data, self.selftest_label = self.test._datasets
def Seq2Seq_RNN(mode): from autoencoder import AutoEncoder_RNN, AE from encoders import encoder_bi from decoders import dynamic_decoder from datamanager import datamanager tmp = [] for fold_id in range(5): autoencoder = AutoEncoder_RNN(encoder_bi, { "hidden_units": [64, 64], "cell_type": "gru" }, dynamic_decoder, { "hidden_units": [64, 64], "cell_type": "gru" }, input_depth=6, output_depth=3, embedding_dim=50, name="encoderdecoder_gru") data = datamanager(time_major=True, expand_dim=False, train_ratio=None, fold_k=5, seed=233) ae = AE(autoencoder, data, 64, "Seq2Seq_GRU/fold_{}".format(fold_id)) if mode == 'train': ae.train(epoches=100) elif mode == 'test': ae.saver.restore(ae.sess, os.path.join(ae.model_dir, "model.ckpt-10699")) a, b = ae.test() tmp.append([a, b]) tf.reset_default_graph() print tmp
def test_AE_init(self): # Check the construction of a AE object input_shape = 784 hidden = [128, 2] act_func = 'tanh' out_func = 'sigmoid' use_BN = True is_gauss_dist = 'True' device = 'cuda' fd = './model/torch' ae = AE(input_shape ,hidden, act_func=act_func, out_func=out_func, use_BN=use_BN, folder=fd, is_gauss_dist=is_gauss_dist, device=device) self.assertIsNotNone(ae)
def Seq2Seq_CNN(mode): from autoencoder import ConverterA_CNN, AE from encoders import encoder_bi from decoders import dynamic_decoder from datamanager import datamanager for fold_id in range(5): autoencoder = ConverterA_CNN(name="encoderdecoder_CNN") data = datamanager(time_major=False, expand_dim=True, train_ratio=None, fold_k=5, seed=233) ae = AE(autoencoder, data, 64, "Seq2Seq_CNN/fold_{}".format(fold_id)) if mode == 'train': ae.train(epoches=100) elif mode == 'test': # ae.saver.restore(ae.sess, os.path.join(ae.model_dir, "model.ckpt-10699")) ae.load_model() ae.test() tf.reset_default_graph()
class Agent(AgentBase): def __init__(self, train_ae=True, load_policy=False, learning_rate=0.001, dim_a=3, fc_layers_neurons=100, loss_function_type='mean_squared', policy_loc='./racing_car_m2/network', image_size=64, action_upper_limits='1,1', action_lower_limits='-1,-1', e='1', ae_loc='graphs/autoencoder/CarRacing-v0/conv_layers_64x64', show_ae_output=True, show_state=True, resize_observation=True): super(Agent, self).__init__(dim_a=dim_a, policy_loc=policy_loc, action_upper_limits=action_upper_limits, action_lower_limits=action_lower_limits, e=e, load_policy=load_policy, train_ae=train_ae, ae_loc=ae_loc, loss_function_type=loss_function_type, learning_rate=learning_rate, fc_layers_neurons=fc_layers_neurons) # High-dimensional state initialization self.resize_observation = resize_observation self.image_size = image_size self.show_state = show_state self.show_ae_output = show_ae_output if self.show_state: self.state_plot = FastImagePlot(1, np.zeros([image_size, image_size]), image_size, 'Image State', vmax=0.5) if self.show_ae_output: self.ae_output_plot = FastImagePlot(2, np.zeros( [image_size, image_size]), image_size, 'Autoencoder Output', vmax=0.5) def _build_network(self, dim_a, params): with tf.variable_scope('base'): # Initialize graph if params['train_ae']: ae_trainer = TrainAE() ae_trainer.run(train=True, show_performance=True) self.AE = AE(ae_loc=params['ae_loc']) ae_encoder = self.AE.latent_space self.low_dim_input_shape = ae_encoder.get_shape()[1:] self.low_dim_input = tf.placeholder(tf.float32, [ None, self.low_dim_input_shape[0], self.low_dim_input_shape[1], self.low_dim_input_shape[2] ], name='input') self.low_dim_input = tf.identity(self.low_dim_input, name='low_dim_input') # Build fully connected layers self.y, loss = fully_connected_layers( tf.contrib.layers.flatten(self.low_dim_input), dim_a, params['fc_layers_neurons'], params['loss_function_type']) variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'base') self.train_policy = tf.train.GradientDescentOptimizer( learning_rate=params['learning_rate']).minimize(loss, var_list=variables) # Initialize tensorflow init = tf.global_variables_initializer() self.sess = tf.Session() self.sess.run(init) self.saver = tf.train.Saver() def _preprocess_observation(self, observation): if self.resize_observation: observation = cv2.resize(observation, (self.image_size, self.image_size)) self.high_dim_observation = observation_to_gray( observation, self.image_size) self.low_dim_observation = self.AE.conv_representation( self.high_dim_observation) # obtain latent space from AE self.network_input = self.low_dim_observation def _refresh_image_plots(self, t): if t % 4 == 0 and self.show_state: self.state_plot.refresh(self.high_dim_observation) if (t + 2) % 4 == 0 and self.show_ae_output: self.ae_output_plot.refresh( self.AE.output(self.high_dim_observation)) def time_step(self, t): self._refresh_image_plots(t)
#Embedding of the reviews: #first the reviews are preprocessed, than embeded, than each embedding is appended to the list X print("embedding") X =[] directory = 'aclImdb/train/unsup1' for filename in os.listdir(directory): if filename.endswith(".txt"): reviews_train = preprocess_reviews(directory+'/'+filename) embed = embed_review(reviews_train) X.append(embed) print("finished") #the list has to be padded, so that every review has the same dimension #reviews with under 300 words will be padded with zeroes padded = pad_sequence(X, batch_first=True) #batch_fisrt=True to keep the order #initialising of the autoencoder that will reduce the last dimension to 50 during the training model = AE(200,100,50) loss = nn.MSELoss() train(model, padded, loss) #extracting the sparse embeddings after the training code = model.encoder(padded) #training the clusters on the pre-trained embeddings kmeans = KMeans(n_clusters=2, random_state=25) dim_red = code.view(len(code),-1) y_pred = kmeans.fit_predict(dim_red.detach())
class TestAutoEncoder(unittest.TestCase): def setUp(self): # construction of Encoder_Decoder object hidden = [128, 2] act_func = 'tanh' out_func = 'sigmoid' use_BN = True init_method = nn.init.xavier_uniform_ is_gauss_dist = 'True' device = 'cuda' self.ed = Encoder_Decoder(hidden, act_func, out_func, use_BN, init_method, is_gauss_dist=is_gauss_dist, device=device) # construction of AutoEncoder object input_shape = 784 fd = './model/torch' self.ae = AE(input_shape ,hidden, act_func=act_func, out_func=out_func, use_BN=use_BN, folder=fd, is_gauss_dist=is_gauss_dist, device=device) # torchのMNISTの呼び出し方がよくわからんかったのでchainerで代用 # MNISTデータの読み込み import chainer self.train, self.test = chainer.datasets.get_mnist() # データとラベルに分割 self.train_data, self.train_label = self.train._datasets self.test_data, self.selftest_label = self.test._datasets def test_Encoder_Decoder_init(self): # Check the construction of an Encoder_Decoder object hidden = [128, 2] act_func = 'tanh' out_func = 'sigmoid' use_BN = True init_method = nn.init.xavier_uniform_ is_gauss_dist = 'True' device = 'cuda' ed = Encoder_Decoder(hidden, act_func, out_func, use_BN, init_method, is_gauss_dist=is_gauss_dist, device=device) self.assertIsNotNone(ed) def test_AE_init(self): # Check the construction of a AE object input_shape = 784 hidden = [128, 2] act_func = 'tanh' out_func = 'sigmoid' use_BN = True is_gauss_dist = 'True' device = 'cuda' fd = './model/torch' ae = AE(input_shape ,hidden, act_func=act_func, out_func=out_func, use_BN=use_BN, folder=fd, is_gauss_dist=is_gauss_dist, device=device) self.assertIsNotNone(ae) def test_train(self): train_mode = 'train' epoch = 3 batchsize = 128 before_state = self.ae.model.state_dict() self.ae.train(self.train_data[:10], epoch, batchsize, C=1.0, k=1, valid=self.test_data, is_plot_weight=True) after_state = {key: value.to('cpu') for key, value in self.ae.model.state_dict().items()} self.assertFalse(before_state.values() == after_state.values()) """ def test_retrain(self): train_mode = 'retrain' epoch = 3 batchsize = 128 self.ae.load_model() before_state = self.ae.model.state_dict() self.ae.train(self.train_data, epoch, batchsize, C=1.0, k=1, valid=None) after_state = {key: value.to('cpu') for key, value in self.ae.model.state_dict().items()} self.assertFalse(before_state.values() == after_state.values()) """ def test_load_model(self): self.assertTrue(callable(self.ae.load_model)) def test_model_to_eval(self): self.assertTrue(callable(self.ae.model_to_eval)) def test_reconst(self): self.assertTrue(callable(self.ae.reconst)) def test_featuremap_to_image(self): self.assertTrue(callable(self.ae.featuremap_to_image))
# 学習条件 # エポックとミニバッチサイズ epoch = 100 batchsize = 128 # 隠れ層のユニット数 hidden = [128] act_func = 'tanh' # 活性化関数 out_func = 'sigmoid' # 出力層の活性化関数 (デコーダの出力層, 無印版は必ずsigmoid) use_BN = False # Batch Normalization を使うか否か fd = './model/torch/' # modelのセットアップ from autoencoder import AE ae = AE(int(train_data.shape[1]), hidden, act_func=act_func, out_func=out_func, use_BN=use_BN, folder=fd, device='cuda') # ed版はこちら. 無印版とは名前が違うだけ. # from variational_autoencoder_ed import VAE_ED # vae = VAE_ED( int(train_data.shape[1]) ,hidden, act_func=act_func, out_func=out_func ,use_BN=True, folder=fd, device='cuda', is_gauss_dist=True) # VAEの学習 if train_mode == 'train': # ae.train(train_data, epoch, batchsize,C=1.0, k=1, valid=None) ae.train(train_data, epoch, batchsize,C=1.0, k=1, valid=test_data, is_plot_weight=True) if train_mode == 'retrain': # もしかしたらoptimizerも保存・ロードしたほうがいいかもしれない ae.load_model() ae.train(train_data, epoch, batchsize,C=1.0, k=1, valid=None) else: