尝试使用未初始化的值训练/ Adam / Variable_22

时间:2019-05-27 16:23:29

标签: python keras model

我有keras错误 我已经在stackoverflow中尝试了许多解决方案,但这不起作用。
我尝试更改tensorflow和keras版本。

泊坞窗中的Windows和Linux(ubuntu和cents)-无法正常工作

(为什么?有些PC会在第一时间工作或操作系统)

除了下面的代码,我还尝试了很多其他方法。

def train_model(self,lr,input_size,dimension):
    K.get_session().run(tf.global_variables_initializer())
    K.get_session().run(tf.local_variables_initializer())
    init = K.tf.global_variables_initializer()
    K.get_session().run(init)

    # K.manual_variable_initialization(True)


    # init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    # sess = K.get_session()
    # sess.run(tf.initialize_all_variables())
    # sess.run(tf.global_variables_initializer())
    # K.get_session(sess).run(tf.local_variables_initializer())
    word_vector = Sequential(name="word_vector")
    tag_one_hot = Sequential(name="tag_one_hot")

    # wordvector
    word_vector.add(Conv2D(16, kernel_size=(2, dimension),activation='relu',padding="SAME",input_shape=[input_size,dimension,1],kernel_regularizer=regularizers.l2(0.001),
                           kernel_initializer= initializers.RandomNormal(mean=0.0, stddev=1e-2, seed=None)))
    word_vector.add(Dropout(0.5))
    word_vector.add(Conv2D(32, (3, dimension), activation='relu', padding="VALID",kernel_regularizer=regularizers.l2(0.001),
                           kernel_initializer=initializers.RandomNormal(mean=0.0, stddev=1e-2, seed=None)))
    word_vector.add(MaxPooling2D(pool_size=(20, 1), strides=(1,1)))
    word_vector.add(Dropout(0.5))
    word_vector.add(Flatten())


    # tagset
    tag_one_hot.add(Dense(256, input_dim=input_size*56, activation='relu',kernel_initializer=initializers.RandomNormal(mean=0.0,
        stddev=1e-2, seed=None)))
    tag_one_hot.add(Dropout(0.5))
    tag_one_hot.add(Dense(256, input_dim=256, activation='relu',kernel_initializer=initializers.RandomNormal(mean=0.0,
        stddev=1e-2, seed=None)))
    tag_one_hot.add(Dropout(0.5))
    tag_one_hot.add(Dense(256, input_dim=256, activation='relu',kernel_initializer=initializers.RandomNormal(mean=0.0,
        stddev=1e-2, seed=None)))
    tag_one_hot.add(Dropout(0.5))
    # merge
    concat = Concatenate(axis=1)([word_vector.output,tag_one_hot.output])
    merged = Dense(2048,activation='relu',kernel_initializer=initializers.RandomNormal(mean=0.0,
        stddev=1e-2, seed=None))(concat)
    merged = Dropout(0.5)(merged)
    merged = Dense(2048, activation='relu', kernel_initializer=initializers.RandomNormal(mean=0.0, stddev=1e-2, seed=None))(merged)
    merged = Dropout(0.5)(merged)
    merged = Dense(1024, activation='relu', kernel_initializer=initializers.RandomNormal(mean=0.0, stddev=1e-2, seed=None))(merged)
    merged = Dropout(0.5)(merged)
    merged = Dense(3, activation='sigmoid', kernel_initializer=initializers.RandomNormal(mean=0.0, stddev=1e-2, seed=None))(merged)
    #last model
    self.model = Model(inputs=[word_vector.input,tag_one_hot.input],outputs=[merged],name="full_model")


    adam = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-8)

    sess =  tf.Session()
    sess.run(tf.global_variables_initializer())
    K.set_session(sess)

    self.model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])

这是错误代码

文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py”,行1039,适合     validate_steps = validation_steps)   在fit_loop中的文件``C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training_arrays.py'',第199行     outs = f(ins_batch)   在调用中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ backend \ tensorflow_backend.py”,第2715行     返回self._call(输入)   _call中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ backend \ tensorflow_backend.py”,行2675     获取= self._callable_fn(* array_vals)   调用中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ client \ session.py”,行1454     self._session._session,self._handle,参数,状态,无)   退出中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ framework \ errors_impl.py”,第519行     c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.FailedPreconditionError:尝试使用未初始化的值training / Adam / Variable_22      [[节点:培训/ Adam / Variable_22 / read = IdentityT = DT_FLOAT,_class = [“ loc:@ training / Adam / Assign_13”],_ device =“ / job:localhost /副本:0 / task:0 / device:CPU :0“]]

0 个答案:

没有答案