Keras:UnboundLocalError:局部变量'数组'在赋值之前引用,如何解决这个问题

时间:2017-12-17 06:40:55

标签: arrays pandas tensorflow deep-learning keras

import pandas as pd
import numpy as np

df_train=pd.read_csv('train_titanic.csv')
df_test=pd.read_csv('test_titanic.csv')

df_train.drop(['PassengerId','Name','Ticket','Cabin'],inplace=True,axis=1)
df_test.drop(['PassengerId','Name','Ticket','Cabin'],inplace=True,axis=1)

df_train=pd.concat([df_train,pd.get_dummies(df_train.Sex),pd.get_dummies(df_train.Pclass),pd.get_dummies(df_train.Embarked)],axis=1)
df_test=pd.concat([df_test,pd.get_dummies(df_test.Sex),pd.get_dummies(df_test.Pclass),pd.get_dummies(df_test.Embarked)],axis=1)

df_train.drop(['Sex','Pclass','Embarked','male',3,'S'],axis=1,inplace=True)
df_test.drop(['Sex','Pclass','Embarked','male',3,'S'],axis=1,inplace=True)

df_train.fillna(df_train.mean(),inplace=True)
df_test.fillna(df_test.mean(),inplace=True)

y_train=df_train.iloc[:,0]
X_train=df_train.iloc[:,1:]

def make_model():
    from keras.models import Sequential
    from keras.layers import Dense

    model=Sequential()
    model.add(Dense(10,activation='relu',input_shape=(9,)))
    model.add((Dense(5,activation='relu')))
    model.add((Dense(1,activation='sigmoid')))
    model.compile('adam',loss='binary_crossentropy',metrics=['accuracy'])
    return model

model=make_model()
model.fit(X_train,y_train,batch_size=32,epochs=10)

我收到以下错误:

~\Anaconda3\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    891                               class_weight=class_weight,
    892                               sample_weight=sample_weight,
--> 893                               initial_epoch=initial_epoch)
    894 
    895     def evaluate(self, x, y, batch_size=32, verbose=1,

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1553             class_weight=class_weight,
   1554             check_batch_axis=False,
-> 1555             batch_size=batch_size)
   1556         # Prepare validation data.
   1557         do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
   1407                                     self._feed_input_shapes,
   1408                                     check_batch_axis=False,
-> 1409                                     exception_prefix='input')
   1410         y = _standardize_input_data(y, self._feed_output_names,
   1411                                     output_shapes,

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    124     # Make arrays at least 2D.
    125     for i in range(len(names)):
--> 126         array = arrays[i]
    127         if len(array.shape) == 1:
    128             array = np.expand_dims(array, 1)

UnboundLocalError: local variable 'arrays' referenced before assignment

之前的任何问题都没有任何帮助。这是一个错误吗?我怎么能绕过它呢?它在这个错误中指的是哪个数组?

2 个答案:

答案 0 :(得分:1)

我不得不改变这个

model.fit(X_train,y_train,batch_size=32,epochs=10)

到这个

model.fit(X_train.values,y_train.values,batch_size=32,epochs=10)

答案 1 :(得分:0)

我在带有Ubuntu:16.04映像的docker容器上遇到了相同的错误。 升级pip时出现错误

RUN pip install --upgrade pip

当我对此行发表评论时,tensorflow和keras运行良好:D

这是我的Dockerfile

FROM ubuntu:16.04
RUN \
    apt-get update && \
    apt-get install -y python python-dev python-pip python-virtualenv && \
    rm -rf /var/lib/apt/lists/*
RUN mkdir /src
WORKDIR /src
# RUN pip install --upgrade pip
RUN pip install Werkzeug Flask numpy Keras gevent pillow h5py tensorflow
ADD requirements.txt ./
RUN pip install -r requirements.txt