当前位置: 首页 > 知识库问答 >
问题:

使用梯度下降w. r. t.计算具有theano的神经网络的最佳输入。输入

屠兴旺
2023-03-14

我用k个二进制输入(0,1),一个隐藏层和输出层的一个单元实现并训练了一个神经网络。一旦训练完成,我想获得最大化输出的输入(例如,x使输出层的单位最接近1)。到目前为止,我还没有找到它的实现,因此我正在尝试以下方法:

  1. 列车网络=

对于这些,我用一个玩具示例(k=2)实现了以下代码。基于上的教程http://outlace.com/Beginner-Tutorial-Theano/但改变了向量y,所以只有一个输入组合给出f(x)~ 1,即x=[0,1]。

Edit1:根据建议,优化器设置为无,偏差单位固定为1。第一步:训练神经网络。这运行良好,没有错误。

import os
os.environ["THEANO_FLAGS"] = "optimizer=None"
import theano
import theano.tensor as T
import theano.tensor.nnet as nnet
import numpy as np

x = T.dvector()
y = T.dscalar()

def layer(x, w):
    b = np.array([1], dtype=theano.config.floatX)
    new_x = T.concatenate([x, b])
    m = T.dot(w.T, new_x) #theta1: 3x3 * x: 3x1 = 3x1 ;;; theta2: 1x4 * 4x1
    h = nnet.sigmoid(m)
    return h

def grad_desc(cost, theta):
    alpha = 0.1 #learning rate
    return theta - (alpha * T.grad(cost, wrt=theta))

in_units = 2
hid_units = 3
out_units = 1

theta1 = theano.shared(np.array(np.random.rand(in_units + 1, hid_units), dtype=theano.config.floatX)) # randomly initialize
theta2 = theano.shared(np.array(np.random.rand(hid_units + 1, out_units), dtype=theano.config.floatX))

hid1 = layer(x, theta1) #hidden layer

out1 = T.sum(layer(hid1, theta2)) #output layer
fc = (out1 - y)**2 #cost expression

cost = theano.function(inputs=[x, y], outputs=fc, updates=[
        (theta1, grad_desc(fc, theta1)),
        (theta2, grad_desc(fc, theta2))])
run_forward = theano.function(inputs=[x], outputs=out1)

inputs = np.array([[0,1],[1,0],[1,1],[0,0]]).reshape(4,2) #training data X
exp_y = np.array([1, 0, 0, 0]) #training data Y
cur_cost = 0
for i in range(5000):
    for k in range(len(inputs)):
        cur_cost = cost(inputs[k], exp_y[k]) #call our Theano-compiled cost function, it will auto update weights

print(run_forward([0,1]))

[0,1]的正向运行输出为:0.968905860574。我们还可以使用θ1获得权重值。get\u value()和θ2。get\u value()

第2步:定义神经网络函数f(x)。训练权重(theta1, theta2)是该函数的常数参数。

由于偏置单元是输入x向量的一部分,这里的情况变得有点棘手。为此,我连接了b和x。但代码现在运行良好。

b = np.array([[1]], dtype=theano.config.floatX)
#b_sh = theano.shared(np.array([[1]], dtype=theano.config.floatX))
rand_init = np.random.rand(in_units, 1)
rand_init[0] = 1
x_sh = theano.shared(np.array(rand_init, dtype=theano.config.floatX))
th1 = T.dmatrix()
th2 = T.dmatrix()

nn_hid = T.nnet.sigmoid( T.dot(th1, T.concatenate([x_sh, b])) )
nn_predict = T.sum( T.nnet.sigmoid( T.dot(th2, T.concatenate([nn_hid, b]))))

第3步:问题现在处于梯度下降状态,因为不限于0和1之间的值。fc2=(nn_predict-1)**2

cost3 = theano.function(inputs=[th1, th2], outputs=fc2, updates=[
        (x_sh, grad_desc(fc2, x_sh))])
run_forward = theano.function(inputs=[th1, th2], outputs=nn_predict)

cur_cost = 0
for i in range(10000):

cur_cost = cost3(theta1.get_value().T, theta2.get_value().T) #call our Theano-compiled cost function, it will auto update weights
if i % 500 == 0: #only print the cost every 500 epochs/iterations (to save space)
    print('Cost: %s' % (cur_cost,))
    print x_sh.get_value()

最后一次迭代打印:成本:0.000220317356533[[-0.11492753][1.99729555]]

此外,输入1不断变负,输入2增加,而最优解为[0,1]。如何修复此问题?

共有1个答案

赏彭薄
2023-03-14

您正在通过广播规则添加b=[1],而不是将其串联。此外,一旦将其连接起来,x\u sh就有一个维度到多个维度,这就是为什么错误发生在nn\u predict而不是nn\u hid

 类似资料:
  • 我想提供以下形状的神经网络输入:每个训练条目都是一个维度为700x10的2D数组。总共有204个训练条目。标签只是204大小的一维数组(二进制输出) 我试图只使用密集层: 但是我得到了以下错误(与第一层上的input\u形状无关,但在输出验证期间): 204-训练数据量。 堆栈跟踪: 调试Keras代码时发现: 培训前验证失败。它验证输出数组。 根据神经网络的结构,第一个密集层以某种方式产生700

  • 我目前正在做激光切割的过程优化——在MATLAB中。我试图将工艺参数与切割质量联系起来,例如: 输入(工艺参数) 切割速度 激光功率 辅助气体压力 输出(质量参数) 切割深度 切割宽度 我首先训练一个神经网络模型,以便根据工艺参数预测切削质量。 这很好,现在我对表演不感兴趣。 接下来我想使用遗传算法优化(最大化)输入参数切割速度。这意味着我的适应度函数(目标函数)是1/切割速度。 我为我的适应度函

  • 我对TensorFlow和LSTM架构相当陌生。我在计算数据集的输入和输出(x_train、x_test、y_trainy_test)时遇到了问题。 我最初输入的形状: X_列车:(366,4) Ytrain和Ytest是一系列股票价格。Xtrain和Xtest是我想学习的四个预测股价的功能。 这是产生的错误: -------------------------------------------

  • 为了对图像进行分类,我们使用了一个带有几个卷积层和几个全连接层的神经网络。 元数据有一些数字信息可以帮助对图像进行分类。有没有一种简单的方法可以将数值元数据连同卷积的输出一起输入到第一个全连接层中?有没有可能使用TensorFlow甚至更好的Keras来实现这一点?

  • 我正在尝试创建一个CNN来对数据进行分类。我的数据是X[N\u数据,N\u特征]我想创建一个能够对其进行分类的神经网络。我的问题是关于keras后端Conv1D的输入形状。 我想在上面重复一个过滤器。。假设有10个特征,然后为接下来的10个特征保持相同的权重。对于每个数据,我的卷积层将创建N\U特征/10个新神经元。我该怎么做?我应该在input\u形状中放置什么? 有什么建议吗?非常感谢。

  • 我正在设计一个学习如何玩跳棋游戏的前馈神经网络。 对于输入,必须给出棋盘,输出应该给出赢与输的概率。但是什么是理想的棋盘到一排数字的转换?有32个可能的方块,每个方块上有5种不同的可能性(国王或一块白或黑玩家和自由位置)。如果我为每个正方形的每个可能值提供一个输入单位,那么它将是32 * 5。另一种选择是: 在这种情况下,输入长度将仅为 64,但我不确定哪一个会给出更好的结果。谁能对此给出任何见解