def create_actor_network(self): with tf.variable_scope(self.scope + '-actor'): inputs = tflearn.input_data( shape=[None, self.s_dim[0], self.s_dim[1]]) split_array = [] for i in range(self.s_dim[0]): tmp = tf.reshape(inputs[:, i:i + 1, :], (-1, self.s_dim[1], 1)) split = tflearn.conv_1d(tmp, FEATURE_NUM // 4, KERNEL, activation='relu') split = tflearn.avg_pool_1d(split, 2) split = tflearn.conv_1d(tmp, FEATURE_NUM // 2, KERNEL, activation='relu') split = tflearn.avg_pool_1d(split, 2) split = tflearn.conv_1d(tmp, FEATURE_NUM, KERNEL, activation='relu') #split = tflearn.avg_pool_1d(split, 2) flattern = tflearn.flatten(split) split_array.append(flattern) dense_net_0 = tflearn.merge(split_array, 'concat') dense_net_0 = tflearn.fully_connected(dense_net_0, FEATURE_NUM, activation='relu') out = tflearn.fully_connected(dense_net_0, self.a_dim, activation='softmax') return inputs, out
def cnn(eps, lr, shape): import tflearn inputs = tflearn.input_data(shape=shape) net = tflearn.conv_1d(inputs, nb_filter=64, filter_size=3, strides=1, padding='same', activation='tanh') net = tflearn.avg_pool_1d(net, kernel_size=3) net = tflearn.conv_1d(net, 64, 3, 1, padding='same', activation='tanh') shape = net.get_shape().as_list() print(shape) net = tflearn.reshape(net, [-1, shape[1] * shape[2]]) net = tflearn.normalization.batch_normalization(net) net = tflearn.fully_connected(net, 512, activation='tanh', regularizer='L2') net = tflearn.dropout(net, 0.8) net = tflearn.normalization.batch_normalization(net) net = tflearn.fully_connected(net, 1024, activation='tanh', regularizer='L2') net = tflearn.dropout(net, 0.8) net = tflearn.normalization.batch_normalization(net) net = tflearn.fully_connected(net, 512, activation='tanh', regularizer='L2') softmax = tflearn.fully_connected(net, 20, activation='softmax') # sgd = tflearn.SGD(learning_rate=lr, lr_decay=0.96, decay_step=15) adam = tflearn.optimizers.adam(epsilon=eps, learning_rate=lr) regression = tflearn.regression(softmax, optimizer=adam, metric='accuracy', loss='categorical_crossentropy') model = tflearn.DNN(regression, tensorboard_verbose=3, tensorboard_dir='.', best_val_accuracy=0.7, best_checkpoint_path='./bestFinal/cnn', checkpoint_path='./checkpoints/cnn', max_checkpoints=10) print('Model created') return model
import tflearn import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Building Residual Network # | Adam | epoch: 040 | loss: 0.03133 - acc: 0.9901 | val_loss: 0.06419 - val_acc: 0.9819 -- iter: 5206/5206 net = tflearn.input_data(shape=[None, 121, 35]) net = tflearn.max_pool_1d(net, 4) net = tflearn.conv_1d(net, 128, 6, activation='relu') net = tflearn.max_pool_1d(net, 2) net = tflearn.conv_1d(net, 128, 3, activation='relu') net = tflearn.avg_pool_1d(net, 2) net = tflearn.fully_connected(net, 128, activation='relu') net = tflearn.dropout(net, 0.7) net = tflearn.fully_connected(net, 14, activation='softmax') net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.005) # Train model model = tflearn.DNN(net) model.fit(X, Y, n_epoch=50, validation_set=(X_test, Y_test), show_metric=True)
def get_deep_centerwave_feature(test_data): tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) _, n_dim = test_data.shape ############################### model net = tflearn.input_data(shape=[None, n_dim, 1]) print("input", net.get_shape()) net = tflearn.avg_pool_1d(net, kernel_size=5, strides=5) print("avg_pool_1d", net.get_shape()) net = tflearn.conv_1d(net, 64, 16, 2) print("cov1", net.get_shape()) net = tflearn.batch_normalization(net) print("bn1", net.get_shape()) net = tflearn.activation(net, 'relu') print("relu1", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) print("resn6", net.get_shape()) # net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) # print("resn8", net.get_shape()) # net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) # print("resn10", net.get_shape()) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) feature_layer = tflearn.fully_connected(net, 32, activation='sigmoid') print("feature_layer", feature_layer.get_shape()) net = feature_layer net = tflearn.fully_connected(net, 4, activation='softmax') print("dense", net.get_shape()) net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.01) ############################### ### load model = tflearn.DNN(net) model.load( '../model/model_deep_centerwave_0810_all/model_deep_centerwave_resnet') ### create new model, and get features m2 = tflearn.DNN(feature_layer, session=model.session) out_feature = [] pred = [] num_of_test = len(test_data) for i in range(num_of_test): tmp_test_data = test_data[i].reshape([-1, n_dim, 1]) out_feature.append(m2.predict(tmp_test_data)[0]) # pred.append(model.predict(tmp_test_data)[0]) out_feature = np.array(out_feature) # ### eval # print(len(pred), pred[0], all_label[0]) # MyEval.F1Score3_num(pred, all_label[:num_of_test]) return out_feature