在训练我的神经网络时,它在[float(i) for i in X_Test]
给出了这个错误,但是当我将浮动强制转换为X_Train和X_Test时,错误被删除了但我在此处得到了另一个错误ufunc 'add' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
代码的一部分x = T.dmatrix('Inputs')
import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt
from six.moves import cPickle
import csv, math
from numpy import ceil
# Declare constants
HIDDEN_NEURONS = 50
NUM_CLASSES = 3
NUM_FEATURES = 0
LEARNING_RATE = 0.01
XVAL_SIZE = 0.1
NUM_EPOCHS = 10
raw_data = open("wine.data", 'rb')
temp = list(csv.reader(raw_data, delimiter=',', quoting=csv.QUOTE_NONE))
data = np.array(temp)
np.random.shuffle(data)
percentage = 75
lines = 0
labels = data[:,0]
x_actual = data[:,0:]
with open("wine.data") as f:
lines = len(f.readlines())
X_Train = (x_actual[0:int(np.ceil(lines * (percentage/100)))])
X_Test = (x_actual[int(ceil((lines*(percentage/100)))+1):])
[float(i) for i in X_Train]
[float(i) for i in X_Test]
train_label = labels[:int(ceil(lines*(percentage/100)))]
test_label = labels [int(ceil((lines*(percentage/100)+1))):]
LEARNING_RATE = 0.5
NUM_EPOCHS = 200
rng = np.random.RandomState(1234)
NUM_FEATURES = X_Train.shape[1]
# Declare symbols
x = T.dmatrix('Inputs')
y = T.dmatrix('Outputs')
t = T.dmatrix('Target Values')
Netj = T.dmatrix('Net of hidden layer')
Netk = T.dmatrix('Net of output layer')
Aj = T.dmatrix('Activation of hidden layer')
Wj = np.random.rand(NUM_FEATURES, HIDDEN_NEURONS) * 0.01
Wk = np.random.rand(HIDDEN_NEURONS, NUM_CLASSES) * 0.01
Weights = theano.shared(value=np.concatenate((Wj.ravel(), Wk.ravel()), axis=0),
name="Weights ravelled")
# Define equations
Netj = T.dot(x, Weights[0:NUM_FEATURES * HIDDEN_NEURONS]
.reshape((NUM_FEATURES, HIDDEN_NEURONS)))
Aj = T.nnet.sigmoid(Netj)
Aj_test = Aj
y_test = T.nnet.softmax(Aj_test)
cost = T.mean(T.nnet.categorical_crossentropy(y_test, t))
Grads = T.grad(cost, Weights)
# Define Functions
computeCost = theano.function([y_test, t], cost)
forwardProp = theano.function([x], y_test)
forwardProp_test = theano.function([x], y_test)
updates = [(Weights, Weights - LEARNING_RATE * (Grads))]
trainModel = theano.function([x, t], cost, updates=updates)
costs = {'training': list(), 'xval': list()}
for i in range(NUM_EPOCHS):
print "Epoch number: " + str(i + 1)
costs['training'].append(trainModel(X_Train, train_label))
if(i % 10 == 0 and i > 0):
Test_Result = np.argmax(forwardProp_test(X_Test), axis=1)
Score = float(len(np.where(Test_Result == train_label)[0])) / float(
(train_label.shape[0])) * 100
print "The model classified with an accuracy of: %.2f" % (
float(Score)) + "%"
plt.plot(range(NUM_EPOCHS), costs['training'],
range(NUM_EPOCHS), costs['xval'])
plt.show()
Test_Result = np.argmax(forwardProp(X_Test), axis=1)
Score = float(len(np.where(Test_Result == train_label)[0])) / float(
(train_label.shape[0])) * 100
print "The model performed with an accuracy of: %.2f" % (float(Score)) + "%"
答案 0 :(得分:1)
确保您了解
X_Test = x_actual[int(ceil((lines*(percentage/100)))+1):]
产生(()的外层是不必要的)。它可能是一个数组 - 从错误判断,二维。因此迭代它会产生它的行,1d数组。
如果太大而无法打印整体,请打印其shape
和dtype
。 type(X_Test)
也可能有助于验证它是ndarray
。
即使它有效,该表达式也不会将X_Test
更改为浮点数。它只是生成一个新的列表或其他内容。
[float(i) for i in X_Test]
X_Test = X_Test.astype(float)
需要生成一个新的浮点数组。
我认为你的第二个错误与第一个错误无关。你永远不会去。这看起来像Theano
错误,超出了我的专业领域。