x66ccff 2017-01-17 10:16 采纳率: 57.1%
浏览 1997
已结题

Theano 报错:Wrong number of dimensions...

错误出现在:...(l2.b, l2.b - learning__rate * gb2)])
TypeError: ('Bad input argument to theano function with name "C:/Users/Administrator/Desktop/...python/theano/Regularization....py:66" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (200,).')


 import theano
from sklearn.datasets import load_boston
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt

class Layer(object):#定义神经层
    def __init__(self, inputs, in_size, out_size, activation_function=None):
        self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))
        self.b = theano.shared(np.zeros((out_size, )) + 0.1)
        self.Wx_plus_b = T.dot(inputs, self.W) + self.b
        self.activation_function = activation_function
        if activation_function is None:
            self.outputs = self.Wx_plus_b
        else:
            self.outputs = self.activation_function(self.Wx_plus_b)

def minmax_normalization(data):#正则化数据
    xs_max = np.max(data, axis=0)
    xs_min = np.min(data, axis=0)
    xs = (1 - 0) * (data - xs_min) / (xs_max - xs_min) + 0
    return xs

N=400
feats=28
lamda=0.1
np.random.seed(100)
x_data = rng.randn(N, feats)
x_data = minmax_normalization(x_data)
y_data = rng.randint(size=N, low=0, high=2)

x_train, y_train = x_data[:200], y_data[:200]
x_test, y_test = x_data[200:], y_data[200:]


x = T.dmatrix("x")
y = T.dmatrix("y")

l1 = Layer(x, 13, 50, T.tanh)
l2 = Layer(l1.outputs, 50, 1, None)

cost = T.mean(T.square(l2.outputs - y)) + lamda * ((l1.W ** 2).sum() + (l2.W ** 2).sum())  

gW1, gb1, gW2, gb2 = T.grad(cost, [l1.W, l1.b, l2.W, l2.b])

learning_rate = 0.01
train = theano.function(
    inputs=[x, y],
    updates=[(l1.W, l1.W - learning_rate * gW1),
             (l1.b, l1.b - learning_rate * gb1),
             (l2.W, l2.W - learning_rate * gW2),
             (l2.b, l2.b - learning_rate * gb2)])

compute_cost = theano.function(inputs=[x, y], outputs=cost)

train_err_list = []
test_err_list = []
learning_time = []

for i in range(1000):
    train(x_train, y_train)
    if i % 10 == 0:
        # record cost
        train_err_list.append(compute_cost(x_train, y_train))
        test_err_list.append(compute_cost(x_test, y_test))
        learning_time.append(i)

plt.plot(learning_time, train_err_list, 'r-')
plt.plot(learning_time, test_err_list, 'b--')
plt.show()

__

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2017-01-21 14:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器