Keras model.fit UnboundLocalError [英] Keras model.fit UnboundLocalError
问题描述
我还是不熟悉keras和python,但遇到了我似乎无法理解的错误. 错误是:
I'm still new to keras and python, and I'm getting an error I can't seem to understand. The error is:
Traceback (most recent call last):
File "/Users/N/PycharmProjects/hw2/hw2_1.py", line 35, in <module>
model.fit(trainingInp, trainingOut, epochs=10, batch_size=1)
File "/Library/Python/2.7/site-packages/keras/models.py", line 893, in fit
initial_epoch=initial_epoch)
File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 1555, in fit
batch_size=batch_size)
File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 1409, in _standardize_user_data
exception_prefix='input')
File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 126, in _standardize_input_data
array = arrays[i]
UnboundLocalError: local variable 'arrays' referenced before assignment
它发生在model.fit()中.我的模型是这样的:
It happens in model.fit(). My model is like so:
model = Sequential()
model.add(Dense(3, activation='sigmoid', input_dim=8))
model.add(Dropout(0.5))
model.add(Dense(10, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
print trainingInp
print trainingOut
model.fit(trainingInp, trainingOut, epochs=10, batch_size=1)
我打印数据以确保没有传入空数据,并且在进入model.fit()之前它可以正确打印.
I print my data to make sure I'm not passing empty data in, and it prints correctly just before going into model.fit().
我不太确定如何解决它,因为我真的不知道问题出在哪里.看来问题出在batch_size上,但我认为允许的批处理大小为1.
I'm not quite sure how to fix it as I don't really know what the problem is. It seems like the problem is batch_size, but I thought a batch size of 1 is allowed.
这是我获取数据的方式.我保证数据没有任何空值.
Here is how I get my data. I am guaranteed that the data doesn't have any empty values.
#read and categorize data
data = pandas.read_csv('cars.data.txt', delim_whitespace=True, header=None)
#turn class into an integer
enc = pandas.factorize(data['class'])
data["enc"] = enc[0]
#split the data set and make class into a matrix of outputs
trainingSet, testingSet = train_test_split(data, test_size=0.3)
trainingInp = trainingSet.iloc[:,1:9]
trainingOut = keras.utils.to_categorical(trainingSet['enc'], num_classes=10)
testingInp = testingSet.iloc[:,1:9]
testingOut = keras.utils.to_categorical(testingSet['enc'], num_classes=10)
推荐答案
看起来像Keras中的错误.
Looks like a bug in Keras.
在engine/training.py
中,
elif data.__class__.__name__ == 'DataFrame':
# test if data is a DataFrame, without pandas installed
data = data.values
应该是
elif data.__class__.__name__ == 'DataFrame':
# test if data is a DataFrame, without pandas installed
arrays = data.values
这是我的方法:
UnboundLocalError
表示未定义变量-实际上,这始终是编程错误.故障线是其中的一部分,在使用变量之前不会检查任何条件.因此,代码假定必须始终在这一点上对其进行定义.
UnboundLocalError
means that the variable is not defined -- which virtually always is a programming error. The block that the faulty line is a part of does not check any conditions prior to using the variable. So, the code assumes that it must be always defined by this point.
从故障行向上搜索"arrays
",表明它是在大型if
块的分支中定义的.因此,每个分支最终都应在其工作过程中分配此变量.确实,除了这一点,他们都做了.因此,执行该分支是唯一可能导致变量未定义的唯一方法.
Searching "arrays
" up from the faulty line shows that it's being defined in branches of a large if
block. So, every branch should end up assigning this variable in the course of its work. And indeed, they all do, except this one. So, the execution having taken this branch is the only way how the variable could have ended up being undefined.
现在,剩下的就是找出该分支中的预期代码应该是什么.看到了
Now, all that leaves is find out what the intended code in that branch should be. Seeing that
- 所有其他分支本身以
arrays = <something>
结尾,这一行看起来像它,并且 - 重新分配
data
在这里是没有意义的操作:- 它在不同分支中具有不同的类型(例如,一个
arrays = data
和另一个arrays = [data]
) - 在任何一个中都没有重新分配,因此
if
块不应该将其转换为某种通用表示形式-因此,很可能不再使用它了.
- all other branches end with
arrays = <something>
by itself, and this line looks just like it, and - reassigning
data
is a pointless operation here:- it has different types in different branches (e.g. in one
arrays = data
and in anotherarrays = [data]
) - is not reassigned in any of them, so the
if
block is not supposed to convert it to some common representation - as a result, it's most probably is not used further on
代码的作者很可能会打错字,这就是他们必须要写的那行. 查找
Pandas.DataFrame.values
确认它是数组的数组,因此将其直接分配给称为数组"的对象看起来是合法的.the code's author most likely made a typo, and this is what they must've intended the line to be. Looking up
Pandas.DataFrame.values
confirms that it is an array of arrays, so a direct assignment of it to something called "arrays" looks legit.这篇关于Keras model.fit UnboundLocalError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- it has different types in different branches (e.g. in one
- 它在不同分支中具有不同的类型(例如,一个