gpt4 book ai didi

tensorflow - 如何在 MLP 中创建可变层数

转载 作者:行者123 更新时间:2023-12-03 22:49:57 36 4
gpt4 key购买 nike

最近我正在使用 Tensorflow。我正在探索如何在 Tensorflow 中实现多层感知器。

我在网上学习了很多教程。他们中的大多数使用一两个隐藏层。一个简单的例子取自 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 不支持赋值操作。那么设计这种东西的正确方法是什么。

最佳答案

你可以做这样的事情而不是你的循环:

    last_layer=x
for idx,tot_neu in enumerate(h_archi):
assert( tot_neu > 0 )
layer_no = idx+1
r,c = last_layer.get_shape()
weights_ = tf.Variable(tf.random_normal([c,tot_neu]),name="hid_{0}_{1}".format(layer_no-1,layer_no))
bias_ = tf.Variable(tf.random_normal([tot_neu]), name="bias_{0}".format(layer_no))
last_layer = tf.add(tf.matmul(last_layer,weights_),bias_)
r,c = last_layer.get_shape()

如果您需要访问中间张量(偏差、权重、层等),您可以将它们存储在列表中的每个步骤中,例如

关于tensorflow - 如何在 MLP 中创建可变层数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828790/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com