{"action":"created","issue":{"url":"https://api.github.com/repos/MoyanZitto/keras-cn/issues/19","repository_url":"https://api.github.com/repos/MoyanZitto/keras-cn","labels_url":"https://api.github.com/repos/MoyanZitto/keras-cn/issues/19/labels{/name}","comments_url":"https://api.github.com/repos/MoyanZitto/keras-cn/issues/19/comments","events_url":"https://api.github.com/repos/MoyanZitto/keras-cn/issues/19/events","html_url":"https://github.com/MoyanZitto/keras-cn/issues/19","id":183941562,"number":19,"title":"怎么将预训练的权重载入新的模型","user":{"login":"zhangyuxin621","id":22066519,"avatar_url":"https://avatars.githubusercontent.com/u/22066519?v=3","gravatar_id":"","url":"https://api.github.com/users/zhangyuxin621","html_url":"https://github.com/zhangyuxin621","followers_url":"https://api.github.com/users/zhangyuxin621/followers","following_url":"https://api.github.com/users/zhangyuxin621/following{/other_user}","gists_url":"https://api.github.com/users/zhangyuxin621/gists{/gist_id}","starred_url":"https://api.github.com/users/zhangyuxin621/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhangyuxin621/subscriptions","organizations_url":"https://api.github.com/users/zhangyuxin621/orgs","repos_url":"https://api.github.com/users/zhangyuxin621/repos","events_url":"https://api.github.com/users/zhangyuxin621/events{/privacy}","received_events_url":"https://api.github.com/users/zhangyuxin621/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-19T11:58:17Z","updated_at":"2016-10-19T13:04:05Z","closed_at":null,"body":"我想写一个DNN的模型，用自编码器预训练\r\ninput_img = Input(shape=(39,))\r\nencoded = Dense(32, activation='relu')(input_img)\r\nencoded = Dense(16, activation='relu')(encoded)\r\nencoded = Dense(8, activation='relu')(encoded)\r\n\r\ndecoded = Dense(16, activation='relu')(encoded)\r\ndecoded = Dense(32, activation='relu')(decoded)\r\ndecoded = Dense(39, activation='softmax')(decoded)\r\n\r\nautoencoder = Model(input=input_img, output=decoded)\r\nautoencoder.compile(optimizer='adadelta', loss='categorical_crossentropy')\r\n这是预训练的模型，一共六层，保存在h5py中\r\nh5py里的格式大概是这样的\r\nHDF5 My.h5 \r\nGroup '/' \r\n    Attributes:\r\n        'layer_names':  'input_1', 'dense_1', 'dense_2', 'dense_3', 'dense_4', 'dense_5', 'dense_6'\r\n    Group '/dense_1' \r\n        Attributes:\r\n            'weight_names':  'dense_1_W:0', 'dense_1_b:0'\r\n        Dataset 'dense_1_W:0' \r\n            Size:  32x39\r\n            MaxSize:  32x39\r\n            Datatype:   H5T_IEEE_F32LE (single)\r\n            ChunkSize:  []\r\n            Filters:  none\r\n            FillValue:  0.000000\r\n        Dataset 'dense_1_b:0' \r\n            Size:  32\r\n            MaxSize:  32\r\n            Datatype:   H5T_IEEE_F32LE (single)\r\n            ChunkSize:  []\r\n            Filters:  none\r\n            FillValue:  0.000000\r\n现在我用DNN又写了一个模型\r\nmodel=Sequential()\r\ninput_img = Input(shape=(39,))\r\nmodel = Dropout(0.2)(input_img)\r\nmodel = Dense(32, activation='relu')(model)\r\nmodel = Dropout(0.2)(model)\r\nmodel = Dense(16, activation='relu')(model)\r\nmodel = Dropout(0.2)(model)\r\nmodel = Dense(8, activation='relu')(model)\r\nmodel = Dropout(0.2)(model)\r\nmodel = Dense(4, activation='softmax')(model)\r\nDNN = Model(input=input_img, output=model)\r\nsgd = SGD(lr=0.01, momentum=0.8, decay=0.0, nesterov=False);\r\nDNN.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy'])\r\n\r\nweights_path='.../My.h5'\r\n\r\nDNN.load_weights('.../My.h5',True)\r\n提示我不能将自编码器的8节点到16节点放入DNN的8节点到4节点，ValueError: Shapes (8, 4) and (8, 16) are not compatible\r\nkeras更新到1.1.0不是可以将权重载入到不同模型中吗，我该怎么设置才能载入进去？"},"comment":{"url":"https://api.github.com/repos/MoyanZitto/keras-cn/issues/comments/254806093","html_url":"https://github.com/MoyanZitto/keras-cn/issues/19#issuecomment-254806093","issue_url":"https://api.github.com/repos/MoyanZitto/keras-cn/issues/19","id":254806093,"user":{"login":"MoyanZitto","id":10725096,"avatar_url":"https://avatars.githubusercontent.com/u/10725096?v=3","gravatar_id":"","url":"https://api.github.com/users/MoyanZitto","html_url":"https://github.com/MoyanZitto","followers_url":"https://api.github.com/users/MoyanZitto/followers","following_url":"https://api.github.com/users/MoyanZitto/following{/other_user}","gists_url":"https://api.github.com/users/MoyanZitto/gists{/gist_id}","starred_url":"https://api.github.com/users/MoyanZitto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MoyanZitto/subscriptions","organizations_url":"https://api.github.com/users/MoyanZitto/orgs","repos_url":"https://api.github.com/users/MoyanZitto/repos","events_url":"https://api.github.com/users/MoyanZitto/events{/privacy}","received_events_url":"https://api.github.com/users/MoyanZitto/received_events","type":"User","site_admin":false},"created_at":"2016-10-19T13:04:05Z","updated_at":"2016-10-19T13:04:05Z","body":"@zhangyuxin621 \r\n同学，载入不同模型的前提是名字要匹配，你可以print一下layer的name，看看能不能匹配的起？\r\n\r\n最好将你关心的模型起下名字，比如Dense(32, name='fc1')什么的，这样就没问题了。\r\n\r\n另外load_weights请使用关键在by_name，而不要只写一个True，目前这个写法没有错误，但不使用关键字存在参数匹配错误的风险。"}}
{"actor":{"display_login":"MoyanZitto"}}