• keras输出中间层结果,某一层的权重、偏置


     转载:https://blog.csdn.net/hahajinbu/article/details/77982721

    from keras.models import Sequential,Model
    from keras.layers import Dense
    import numpy as np

    model = Sequential()
    model.add(Dense(32,activation="relu",input_dim=100))
    model.add(Dense(16,activation="relu",name="Dense_1"))
    model.add(Dense(1, activation='sigmoid',name="Dense_2"))

    model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])


    #假设训练和测试使用同一组数据
    data = np.random.random((1000, 100))
    labels = np.random.randint(2, size=(1000, 1))

    model.fit(data,labels,epochs=10,batch_size=32)

    #取某一层的输出为输出新建为model,采用函数模型
    dense1_layer_model = Model(inputs=model.input,outputs=model.get_layer('Dense_1').output)
    dense1_output = dense1_layer_model.predict(data)

    print (dense1_output.shape)


    #获得某一层的权重和偏置
    weight_Dense_1,bias_Dense_1 = model.get_layer('Dense_1').get_weights()

    print(weight_Dense_1.shape)
    print(bias_Dense_1.shape)

  • 相关阅读:
    Tensor总结
    Tensorflow池化
    conda操作
    KS值计算
    supervisor实践
    npm/yarn实践
    nni 环境搭建
    阿里云个人邮箱配置
    Jinja2宏使用
    利用VS code 远程调试 docker 中的 dotnet 应用
  • 原文地址:https://www.cnblogs.com/wzdLY/p/9683657.html
Copyright © 2020-2023  润新知