小编Mas*_*EET的帖子

尝试使用张量流自定义回调获取中间层预测时出现“层未连接,无输入可返回”错误

我正在尝试使用自定义回调在训练期间访问模型中间层的预测。以下实际代码的精简版本演示了该问题。

import tensorflow as tf
import numpy as np

class Model(tf.keras.Model):
    def __init__(self, input_shape=None, name="cus_model", **kwargs):
        super(Model, self).__init__(name=name, **kwargs)
        
    def build(self, input_shape):
        self.dense1 = tf.keras.layers.Dense(input_shape=input_shape, units=32)
        
    def call(self, input_tensor):
        return self.dense1(input_tensor)

class CustomCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        get_output = tf.keras.backend.function(
            inputs = self.model.layers[0].input,
            outputs = self.model.layers[0].output
        )
        print("Layer output: ",get_output.outputs)

X = np.ones((8,16))
y = np.sum(X, axis=1)

model = Model()
model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
model.fit(X,y, epochs=8, callbacks=[CustomCallback()])
Run Code Online (Sandbox Code Playgroud)

回调是按照此答案中的建议编写的。出现以下错误:

<ipython-input-3-635fd53dbffc> in on_epoch_end(self, epoch, logs)
     12     def on_epoch_end(self, epoch, logs=None):
     13         get_output …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

8
推荐指数
1
解决办法
5164
查看次数

如何在重用std :: stringstream变量时避免使用std :: stringstream.str()和std :: stringstream.clear()?

operator<<像这样重载:

std::ostream& operator<<(std::ostream& os, SomeClass C){ 
//SomeClass is the class to be represented with operator overloading
os << "{ ";
os << C.getPropertyA() << " ";
os << C.getPropertyB() << " }";
//getPropertyA(), getPropertyB():functions of 'SomeClass' that return std::string
return os;
}
Run Code Online (Sandbox Code Playgroud)

现在我正在使用googletest来测试这样的operator<<重载:

SomeClass C1;
SomeClass C2;
.
.
std::stringstream ss;
std::string str;

//First test
ss << C1;
std::getline(ss, str);
EXPECT_EQ("<some expected value>", str); // googletest test

//Second test
ss.str("");ss.clear(); //Mandatory if 'ss' need …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading stringstream googletest c++11

0
推荐指数
1
解决办法
375
查看次数