zoukankan      html  css  js  c++  java
  • Keras & Theano 输出中间层结果

    Keras & Theano get output of an intermediate layer

    1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict.

    import seaborn as sbn
    import pylab as plt
    import theano
    from keras.models import Sequential
    from keras.layers import Dense,Activation
     
    from keras.models import Model
     
    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'])
     
    # Generate dummy data
    import numpy as np
    #假设训练和测试使用同一组数据
    data = np.random.random((1000, 100))
    labels = np.random.randint(2, size=(1000, 1))
     
    # Train the model, iterating on the data in batches of 32 samples
    model.fit(data, labels, epochs=10, batch_size=32)
    #已有的model在load权重过后
    #取某一层的输出为输出新建为model,采用函数模型
    dense1_layer_model = Model(inputs=model.input,
                                         outputs=model.get_layer('Dense_1').output)
    #以这个model的预测值作为输出
    dense1_output = dense1_layer_model.predict(data)
     
    print(dense1_output.shape)
    print(dense1_output[0])
    2.因为我的后端是使用的theano,所以还可以考虑使用theano的函数:
    #这是一个theano的函数
    dense1 = theano.function([model.layers[0].input],model.layers[1].output,allow_input_downcast=True)
    dense1_output = dense1(data)  #visualize these images's FC-layer feature
    print(dense1_output[0])

    效果应该是一样的。

     
    来源:https://blog.csdn.net/hahajinbu/article/details/77982721
     
     
     
  • 相关阅读:
    FastAPI项目实战: 个人博客项目的API
    Jmeter分布式执行,java.rmi.UnmarshalException: xxxAbstractSimpleThreadGroup错误
    [转]JMeter分布式的坑
    Docker菜鸟教程-硬敲系列
    VMware EXIS 安装
    2020简单总结
    07.1 迭代器、生成器
    locust 的 ‘1’ 版本时代变化
    移动端专项测试-内存泄漏
    乘风破浪的不止姐姐,还有我们的测试工程师!
  • 原文地址:https://www.cnblogs.com/jins-note/p/9767403.html
Copyright © 2011-2022 走看看