2016-09-18 108 views
0

这是我收到的错误: mtrand.RandomState.randint中的文件“mtrand.pyx”,行1192 numpy/random/mtrand/mtrand.c:14128)Theano ANN“TypeError:randint()至少需要1个位置参数(给出0)”

我对编码有点新,但我真的想从简单的ANN开始,所以我决定开始这个项目。

类型错误:randint()采用(给出0)至少1位置参数

# - - 编码:UTF-8 - - “”” 创建于太阳09月18十四时56分44秒2016

@author: Jamoonie 
""" 

##theano practice 

import numpy as np 
import theano 
import theano.tensor as T 
from sklearn.datasets import load_digits 


digits=load_digits() 
print (digits.data.shape) 

train_x = list(digits.data) 
#print train_x.count 

train_x = np.array(train_x) 
#print train_x 

train_y = list(digits.target) 
#print train_y.count 

train_y = np.array(train_y) 
#print train_y 

#q = T.matrix('q')  checking how matrix dot products work, and how the row,col of the W0 should be set up 
#q = np.zeros([5,10]) 
#print q 
#p = T.matrix('p') 
#p = np.zeros([10,5]) 
# 
#print np.dot(q,p) 

nn_input_dim = train_x.shape[1] ## if shape[0] it yields 1797, which is the number of rows 

print nn_input_dim ##shows 64; shape[1] yields 1 row thus 64 columns! which are the layers of data we want to apply 

nn_hdim0 = 10 

nn_output_dim = len(train_y) 

#nn_hdim0 = np.transpose(np.zeros(digits.data.shape)) 

#print nn_hdim0 

epsilon = 0.008 

batch_size = 100 ## how much data input per iteration 

X = T.matrix('X') 
y = T.lvector('y') 

## set weight shapeswith random values 
#W0 = np.transpose(np.zeros(digits.data.shape)) 
W0 = theano.shared(np.random.randn(nn_input_dim,nn_hdim0),name='W0') ##the shape of W0 should be row=input_dim, col=# hidden nodes 

b0 = theano.shared(np.zeros(nn_hdim0),name='b0') 

W1 = theano.shared(np.random.randn(nn_hdim0,nn_output_dim),name='W1') ## shape of W1 should have row=#hidden nodes, col = output dimension 

b1 = theano.shared(np.zeros(nn_output_dim),name='b1') 

z0 = X.dot(W0)+b0 

a0 = T.nnet.softmax(z0) ## first hidden layer result 

z1 = a0.dot(W1)+b1 

a1 = T.nnet.softmax(z1) ## final result or prediction 

loss = T.nnet.categorical_crossentropy(a1,y).mean() ## howmuch the prediction differrs from the real result 

prediction = T.argmax(a1,axis=1) ## the maximum values of a1, presented in index posn 1 

fwd_propagation = theano.function([X],a1) ## forward propagation function dpeneding on the array of X values and final prediction 

calc_loss = theano.function([X,y],loss) 

predict= theano.function([X],prediction) 

accuracy = theano.function([X],T.sum(T.eq(prediction,train_y))) ## T.eq is elementwise. so this does an elementwise sum of prediction and train_y 

dW0 = T.grad(loss,W0) 
dW1 = T.grad(loss,W1) 
db0=T.grad(loss,b0) 
db1=T.grad(loss,b1) 

np.random.randint() 
gradient_step = theano.function(
    [X,y], ##for each set of X,y values 
    updates=((W1,W1-epsilon*dW1),  ##updates W1 by deltaW1(error)*learning rate and subtracting from original W1 
      (W0,W0-epsilon*dW0), 
      (b1,b1-epsilon*db1), 
      (b0,b0-epsilon*db0))) 

def build(iterations = 80000): 
    W1.set_value(np.random.randn(nn_hdim0,nn_output_dim)/np.sqrt(nn_input_dim)) ## why dividing by the sqrt of nn_input_dim,i'm not sure, but they're meant to be random anyway. 
    W0.set_value(np.random.randn(nn_input_dim,nn_hdim0)/np.sqrt(nn_input_dim)) 
    b1.set_value(np.zeros(nn_output_dim)) 
    b0.set_value(np.zeros(nn_hdim0)) 
    for i in range(0, iterations): 
     batch_indicies=np.random.randint(0,17,size=100) 
     batch_x,batch_y=train_x[batch_indicies],train_y[batch_indicies] 
     gradient_step(batch_x,batch_y) 
     ##so we're providing the values now for the weights, biases and input output values 
     if i%2000==0: 
      print("loss after iteration %r: %r" % (i, calc_loss(train_x,train_y))) 
      print(accuracy(train_x)) 
     if i==80000: 
      print (W0,b0,W1,b1) 

build() 

回答

0

作为每documentation,则需要在-至少指定整数的最低值以从分布绘制。如果您希望的随机数小于213(准确地说介于0和213)那么你会做r = np.random.randint(213),如果你想要一个随机数在一些范围之间,我们说213和537然后你会这样做,r = np.random.randint(213, 537)。你也试图从randint(..)得到一个随机数,甚至没有将它存储到任何变量(或传递给任何函数),这是没用的。我建议通过基本的Theano tutorials开始,从here开始。

相关问题