我已经将经过训练的 keras 模型转换为tensorflow.js,现在我正在尝试在react-native中实现它。模型转换得很好,但是当我尝试加载它时,它说
错误:未知激活:swish
我知道可以制作自定义图层,但我不确定这是否适用于已经训练过的模型。
有什么解决办法吗?
代码: Swift.js
import * as tf from '@tensorflow/tfjs';
class Swish extends tf.layers.Layer {
constructor(config) {
super(config);
this.alpha = config.alpha;
}
call(input) {
return tf.tidy(() => {
const x = input[0]; //tf.getExactlyOneTensor(input);
return tf.sigmoid(x.mul(this.alpha)).mul(x);
});
}
computeOutputShape(inputShape){
return inputShape;
}
static get className() {
return 'swish';
}
}
export default Swish;
加载中:
import Swish from '/Users/alex/Documents/GitHub/TrueSkyApp/src/components/swish.js'
const modelJson = require('/Users/alex/Documents/GitHub/TrueSkyApp/assets/model/model.json');
const modelWeights = require('/Users/alex/Documents/GitHub/TrueSkyApp/assets/model/group1-shard1of1.bin');
class TFtest extends Component {
private model: any
state = {
isModelReady: false
}
async componentDidMount() {
await tf.ready()
this.model = await tf.loadLayersModel(bundleResourceIO(modelJson, modelWeights));
this.setState({
isModelReady: true
})
//Output in Expo console
console.log(this.state.isModelReady)
}
render() {
return (
<View style={styles.container}>
<Text>Model ready? {this.state.isModelReady ? <Text>Yes</Text> : ''}</Text>
</View>
)
}
}
最佳答案
即使模型已经经过训练,js 中加载的模型也需要查看层的实现,以了解给定的输入层的输出是什么。
Error: Unknown activation: swish
显然该错误表明需要提供层激活
更新
该类需要注册
tf.serialization.registerClass(Swish)
关于keras - Tensorflow.js 使用自定义层和已训练的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62034331/