{"action":"created","issue":{"url":"https://api.github.com/repos/keras-team/keras/issues/10563","repository_url":"https://api.github.com/repos/keras-team/keras","labels_url":"https://api.github.com/repos/keras-team/keras/issues/10563/labels{/name}","comments_url":"https://api.github.com/repos/keras-team/keras/issues/10563/comments","events_url":"https://api.github.com/repos/keras-team/keras/issues/10563/events","html_url":"https://github.com/keras-team/keras/issues/10563","id":336841563,"node_id":"MDU6SXNzdWUzMzY4NDE1NjM=","number":10563,"title":"ValueError: Error when checking input: expected dense_5_input to have shape (4396,) but got array with shape (4096,)","user":{"login":"correiav","id":26824262,"node_id":"MDQ6VXNlcjI2ODI0MjYy","avatar_url":"https://avatars0.githubusercontent.com/u/26824262?v=4","gravatar_id":"","url":"https://api.github.com/users/correiav","html_url":"https://github.com/correiav","followers_url":"https://api.github.com/users/correiav/followers","following_url":"https://api.github.com/users/correiav/following{/other_user}","gists_url":"https://api.github.com/users/correiav/gists{/gist_id}","starred_url":"https://api.github.com/users/correiav/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/correiav/subscriptions","organizations_url":"https://api.github.com/users/correiav/orgs","repos_url":"https://api.github.com/users/correiav/repos","events_url":"https://api.github.com/users/correiav/events{/privacy}","received_events_url":"https://api.github.com/users/correiav/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-06-29T02:30:01Z","updated_at":"2018-07-02T08:12:18Z","closed_at":null,"author_association":"NONE","body":"Hi all,\r\nI am trying to train a simple MLP model from this repo: https://github.com/avisingh599/visual-qa/blob/master/scripts/trainMLP.py. However, I can't figure out how to fix the error mentioned above. Would appreciate your help, please. Check the code below as I've made minor changes due to Python version divergencies. Many thanks!\r\n\r\n`\r\nfrom keras.models import Sequential\r\nfrom keras.layers.core import Dense, Dropout, Activation\r\nfrom keras.optimizers import SGD\r\nfrom keras.utils import np_utils, generic_utils\r\nfrom sklearn import preprocessing\r\nfrom sklearn.externals import joblib\r\nfrom spacy.lang.en import English\r\nfrom features import get_questions_matrix_sum, get_images_matrix, get_answers_matrix\r\nfrom utils import grouper, selectFrequentAnswers\r\n\r\ndef main():\r\n\r\n\tparser = argparse.ArgumentParser()\r\n\tparser.add_argument('-num_hidden_units', type=int, default=1024)\r\n\tparser.add_argument('-num_hidden_layers', type=int, default=3)\r\n\tparser.add_argument('-dropout', type=float, default=0.5)\r\n\tparser.add_argument('-activation', type=str, default='tanh')\r\n\tparser.add_argument('-language_only', type=bool, default= False)\r\n\tparser.add_argument('-num_epochs', type=int, default=10) #default=100\r\n\tparser.add_argument('-model_save_interval', type=int, default=10)\r\n\tparser.add_argument('-batch_size', type=int, default=128)\r\n\targs = parser.parse_args()\r\n\r\n\tquestions_train = open('data/qa/preprocess/questions_train2014.txt', 'r').read().splitlines()\r\n\tanswers_train = open('data/qa/preprocess/answers_train2014_modal.txt', 'r').read().splitlines()\r\n\timages_train = open('data/qa/preprocess/images_train2014.txt', 'r').read().splitlines()\r\n\tvgg_model_path = 'data/coco/vgg_feats.mat'\r\n\tmaxAnswers = 1000\r\n\tquestions_train, answers_train, images_train = selectFrequentAnswers(questions_train,answers_train,images_train, maxAnswers)\r\n\r\n\t#encode the remaining answers\r\n\tlabelencoder = preprocessing.LabelEncoder()\r\n\tlabelencoder.fit(answers_train)\r\n\tnb_classes = len(list(labelencoder.classes_))\r\n\tjoblib.dump(labelencoder,'models/labelencoder.pkl')\r\n\r\n\tfeatures_struct = scipy.io.loadmat(vgg_model_path)\r\n\tVGGfeatures = features_struct['feats']\r\n\tprint ('loaded vgg features')\r\n\timage_ids = open('data/coco/coco_vgg_IDMap.txt').read().splitlines()\r\n\tid_map = {}\r\n\tfor ids in image_ids:\r\n\t\tid_split = ids.split()\r\n\t\tid_map[id_split[0]] = int(id_split[1])\r\n\r\n\tnlp = English()\r\n\tprint ('loaded word2vec features...')\r\n\timg_dim = 4096\r\n\tword_vec_dim = 300\r\n\r\n\tmodel = Sequential()\r\n\tif args.language_only:\r\n\t\tmodel.add(Dense(args.num_hidden_units, input_dim=word_vec_dim, init='uniform'))\r\n\telse:\r\n\t\tmodel.add(Dense(args.num_hidden_units, input_dim=img_dim+word_vec_dim, init='uniform'))\r\n\tmodel.add(Activation(args.activation))\r\n\tif args.dropout>0:\r\n\t\tmodel.add(Dropout(args.dropout))\r\n\tfor i in range(args.num_hidden_layers-1):\r\n\t\tmodel.add(Dense(args.num_hidden_units, init='uniform'))\r\n\t\tmodel.add(Activation(args.activation))\r\n\t\tif args.dropout>0:\r\n\t\t\tmodel.add(Dropout(args.dropout))\r\n\tmodel.add(Dense(nb_classes, init='uniform'))\r\n\tmodel.add(Activation('softmax'))\r\n\r\n\tjson_string = model.to_json()\r\n\tif args.language_only:\r\n\t\tmodel_file_name = 'models/mlp_language_only_num_hidden_units_' + str(args.num_hidden_units) + '_num_hidden_layers_' + str(args.num_hidden_layers)\r\n\telse:\r\n\t\tmodel_file_name = 'models/mlp_num_hidden_units_' + str(args.num_hidden_units) + '_num_hidden_layers_' + str(args.num_hidden_layers)\t\t\r\n\topen(model_file_name  + '.json', 'w').write(json_string)\r\n\r\n\tprint ('Compiling model...')\r\n\tmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop')\r\n\tprint ('Compilation done...')\r\n\t  \r\n\tprint ('Training started...')\r\n\tfor k in range(args.num_epochs):\r\n\t\t#shuffle the data points before going through them\r\n\t\tindex_shuf = list(range(len(questions_train)))\r\n\t\tshuffle(index_shuf)\r\n\t\tquestions_train = [questions_train[i] for i in index_shuf]\r\n\t\tanswers_train = [answers_train[i] for i in index_shuf]\r\n\t\timages_train = [images_train[i] for i in index_shuf]\r\n\t\tprogbar = generic_utils.Progbar(len(questions_train))\r\n\t\tfor qu_batch,an_batch,im_batch in zip(grouper(questions_train, args.batch_size, fillvalue=questions_train[-1]), \r\n\t\t\t\t\t\t\t\t\t\t\tgrouper(answers_train, args.batch_size, fillvalue=answers_train[-1]), \r\n\t\t\t\t\t\t\t\t\t\t\tgrouper(images_train, args.batch_size, fillvalue=images_train[-1])):\r\n\t\t\tX_q_batch = get_questions_matrix_sum(qu_batch, nlp)\r\n\t\t\tif args.language_only:\r\n\t\t\t\tX_batch = X_q_batch\r\n\t\t\telse:\r\n\t\t\t\tX_i_batch = get_images_matrix(im_batch, id_map, VGGfeatures)\r\n\t\t\t\tX_batch = np.hstack((X_q_batch, X_i_batch))\r\n\t\t\tY_batch = get_answers_matrix(an_batch, labelencoder)\r\n\t\t\tloss = model.train_on_batch(X_batch, Y_batch)            \r\n\t\t\tprogbar.add(args.batch_size, values=[(\"train loss\", loss)])\r\n\t\t#print type(loss)\r\n\t\tif k%args.model_save_interval == 0:\r\n\t\t\tmodel.save_weights(model_file_name + '_epoch_{:02d}.hdf5'.format(k))\r\n\r\n\tmodel.save_weights(model_file_name + '_epoch_{:02d}.hdf5'.format(k))\r\nif __name__ == \"__main__\":\r\n\tmain()\r\n`"},"comment":{"url":"https://api.github.com/repos/keras-team/keras/issues/comments/401706360","html_url":"https://github.com/keras-team/keras/issues/10563#issuecomment-401706360","issue_url":"https://api.github.com/repos/keras-team/keras/issues/10563","id":401706360,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMTcwNjM2MA==","user":{"login":"ty5491003","id":37855928,"node_id":"MDQ6VXNlcjM3ODU1OTI4","avatar_url":"https://avatars1.githubusercontent.com/u/37855928?v=4","gravatar_id":"","url":"https://api.github.com/users/ty5491003","html_url":"https://github.com/ty5491003","followers_url":"https://api.github.com/users/ty5491003/followers","following_url":"https://api.github.com/users/ty5491003/following{/other_user}","gists_url":"https://api.github.com/users/ty5491003/gists{/gist_id}","starred_url":"https://api.github.com/users/ty5491003/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ty5491003/subscriptions","organizations_url":"https://api.github.com/users/ty5491003/orgs","repos_url":"https://api.github.com/users/ty5491003/repos","events_url":"https://api.github.com/users/ty5491003/events{/privacy}","received_events_url":"https://api.github.com/users/ty5491003/received_events","type":"User","site_admin":false},"created_at":"2018-07-02T08:12:18Z","updated_at":"2018-07-02T08:12:18Z","author_association":"NONE","body":"clearlove7?"}}
{"actor":{"display_login":"ty5491003"}}