|
a |
|
b/autoencoder_DCAP.py |
|
|
1 |
|
|
|
2 |
|
|
|
3 |
import tensorflow as tf |
|
|
4 |
import numpy as np |
|
|
5 |
import matplotlib.pyplot as plt |
|
|
6 |
import pandas as pd |
|
|
7 |
with open(r"C:\pypro\brcatest_go.csv", 'r') as f: |
|
|
8 |
data = pd.read_csv(f) |
|
|
9 |
|
|
|
10 |
print(data.shape) |
|
|
11 |
tcga_input=np.transpose(data) |
|
|
12 |
print(tcga_input.shape) |
|
|
13 |
|
|
|
14 |
learning_rate = 0.01 |
|
|
15 |
training_epochs = 10 |
|
|
16 |
batch_size = 50 |
|
|
17 |
display_step = 1 |
|
|
18 |
examples_to_show = 10 |
|
|
19 |
|
|
|
20 |
dropout=0.1 |
|
|
21 |
n_input = 60779 |
|
|
22 |
scale = 0.0001 |
|
|
23 |
# tf Graph input (only pictures) |
|
|
24 |
X = tf.placeholder("float", [None, n_input]) |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
n_hidden_1 = 500 # |
|
|
28 |
n_hidden_2 = 200 # |
|
|
29 |
|
|
|
30 |
weights = { |
|
|
31 |
'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), |
|
|
32 |
'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), |
|
|
33 |
'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])), |
|
|
34 |
'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])), |
|
|
35 |
} |
|
|
36 |
biases = { |
|
|
37 |
'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])), |
|
|
38 |
'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])), |
|
|
39 |
'decoder_b1': tf.Variable(tf.random_normal([n_hidden_1])), |
|
|
40 |
'decoder_b2': tf.Variable(tf.random_normal([n_input])), |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
def encoder(x): |
|
|
45 |
layer_1 = tf.nn.tanh(tf.add(tf.matmul(x, weights['encoder_h1']), |
|
|
46 |
biases['encoder_b1'])) |
|
|
47 |
layer_2 = tf.nn.tanh(tf.add(tf.matmul(layer_1, weights['encoder_h2']), |
|
|
48 |
biases['encoder_b2'])) |
|
|
49 |
return layer_2 |
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
def decoder(x): |
|
|
54 |
layer_1 = tf.nn.tanh(tf.add(tf.matmul(x, weights['decoder_h1']), |
|
|
55 |
biases['decoder_b1'])) |
|
|
56 |
layer_2 = tf.nn.tanh(tf.add(tf.matmul(layer_1, weights['decoder_h2']), |
|
|
57 |
biases['decoder_b2'])) |
|
|
58 |
return layer_2 |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
################################################################## |
|
|
62 |
|
|
|
63 |
fc_1 = tf.layers.dense(inputs=X, units=n_hidden_1, |
|
|
64 |
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=scale)) |
|
|
65 |
fc_1_out = tf.nn.tanh(fc_1) |
|
|
66 |
fc_1_dropout = tf.layers.dropout(inputs=fc_1_out, rate=dropout) |
|
|
67 |
|
|
|
68 |
fc_2 = tf.layers.dense(inputs = fc_1_dropout, units = n_hidden_2, kernel_regularizer= tf.contrib.layers.l2_regularizer(scale=scale)) |
|
|
69 |
fc_2_out = tf.nn.tanh(fc_2) |
|
|
70 |
encoder_op = tf.layers.dropout(inputs=fc_2_out, rate=dropout) |
|
|
71 |
|
|
|
72 |
fc_3 = tf.layers.dense(inputs = encoder_op, units = n_hidden_1, kernel_regularizer= tf.contrib.layers.l2_regularizer(scale=scale)) |
|
|
73 |
fc_3_out = tf.nn.tanh(fc_3) |
|
|
74 |
fc_3_dropout = tf.layers.dropout(inputs=fc_3_out, rate=dropout) |
|
|
75 |
|
|
|
76 |
decoder_op = tf.layers.dense(inputs=fc_3_dropout, units=n_input) |
|
|
77 |
################################################################## |
|
|
78 |
|
|
|
79 |
|
|
|
80 |
y_pred = decoder_op |
|
|
81 |
y_true = X |
|
|
82 |
|
|
|
83 |
|
|
|
84 |
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))#+lossL |
|
|
85 |
l2_loss = tf.losses.get_regularization_loss() |
|
|
86 |
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost+l2_loss) |
|
|
87 |
|
|
|
88 |
with tf.Session() as sess: |
|
|
89 |
# tf.initialize_all_variables() no long valid from |
|
|
90 |
# 2017-03-02 if using tensorflow >= 0.12 |
|
|
91 |
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: |
|
|
92 |
init = tf.initialize_all_variables() |
|
|
93 |
else: |
|
|
94 |
init = tf.global_variables_initializer() |
|
|
95 |
sess.run(init) |
|
|
96 |
|
|
|
97 |
total_batch = int(len(tcga_input)/batch_size) |
|
|
98 |
for epoch in range(training_epochs): |
|
|
99 |
for i in range(total_batch): |
|
|
100 |
# tch_xs, batch_ys = mnist.train.next_batch(batch_size) # max(x) = 1, min(x) = 0 |
|
|
101 |
batch_xs = tcga_input[((i)*batch_size):((i+1)*batch_size)] |
|
|
102 |
# Run optimization op (backprop) and cost op (to get loss value) |
|
|
103 |
_, c = sess.run([optimizer, cost], feed_dict={X: batch_xs}) |
|
|
104 |
if epoch % display_step == 0: |
|
|
105 |
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c)) |
|
|
106 |
if epoch == training_epochs - 1: |
|
|
107 |
fea_output = sess.run([encoder_op], feed_dict={X: tcga_input}) |
|
|
108 |
# print(fea_output) |
|
|
109 |
print(np.array(fea_output).shape) |
|
|
110 |
np.savetxt(r'C:\pypro\fea.csv', np.array(fea_output[0]), delimiter=',') |
|
|
111 |
print("Optimization Finished!") |
|
|
112 |
|
|
|
113 |
|