4
最近我正在使用Tensorflow。我正在探索如何在Tensorflow中實現多層Perceptron。如何在MLP中創建可變數量的圖層
我在網上通過了很多教程。他們大多數利用一個或兩個隱藏層。一個簡單的例子取自here
def forwardprop(X, w_1, w_2):
"""
Forward-propagation.
IMPORTANT: yhat is not softmax since TensorFlow's
softmax_cross_entropy_with_logits() does that internally.
"""
h = tf.nn.sigmoid(tf.matmul(X, w_1)) # The \sigma function
yhat = tf.matmul(h, w_2) # The \varphi function
return yhat
X = tf.placeholder("float", shape=[None, x_size])
y = tf.placeholder("float", shape=[None, y_size])
# Weight initializations
w_1 = init_weights((x_size, h_size))
w_2 = init_weights((h_size, y_size))
# Forward propagation
out = forwardprop(X, w_1, w_2)
在這段代碼中,有一個隱藏層。現在我想知道如果我想構建一個可變數量的分層全連接神經網絡。
假設列表h_archi = [100 150 100 50]其中每個值表示第i層神經元的數量(在這種情況下層的總數爲4)。因此,對於層實現的變量數,我編寫了以下醜陋的代碼,
emb_vec = tf.Variable(tf.random_normal([vocabulary_size, EMBEDDING_DIM]), name="emb_vec")
tot_layer = len(h_archi)
op = np.zeros(tot_layer+1)
hid_rep = np.zeros(tot_layer+1)
bias = np.zeros(tot_layer+1)
op[0] = tf.matmul(x, emb_vec)
for idx,tot_neu in enumerate(h_archi):
assert(tot_neu > 0)
layer_no = idx+1
r,c = op[layer_no-1].get_shape()
hid_rep[layer_no] = tf.Variable(tf.random_normal([c,tot_neu]),name="hid_{0}_{1}".format(layer_no-1,layer_no))
bias[layer_no] = tf.Variable(tf.random_normal([tot_neu]), name="bias_{0}".format(layer_no))
op[layer_no] = tf.add(tf.matmul(op[layer_no-1],hid_rep[layer_no]),bias[layer_no])
r,c = op[tot_layer].get_shape()
last_layer = tf.Variable(tf.random_normal([c,output_size]),name="hid_{0}_{1}".format(tot_layer,"last_layer"))
bias_last = tf.Variable(tf.random_normal([output_size]), name="bias_last")
output = tf.add(tf.matmul(op[tot_layer],last_layer))
prediction = tf.nn.softmax(output)
此代碼是完全錯誤的,因爲tensorflow不支持賦值操作。那麼設計這種東西的正確方法是什麼?
非常感謝您的幫助。 –