# Keras 學習札記 ###### tags: `Keras學習筆記`,`LSTM`,`beginner` ## 建NN with LSTM ### LSTM input shape: 三維(batch_size,timesteps,input_dim) units: 指在一層NN中LSTM的單元數量 (參考資料: [In Keras, what exactly am I configuring when I create a stateful `LSTM` layer with N `units`?](https://stackoverflow.com/questions/44273249/in-keras-what-exactly-am-i-configuring-when-i-create-a-stateful-lstm-layer-wi) [Input and Output shape in LSTM (Keras)](https://www.kaggle.com/shivajbd/input-and-output-shape-in-lstm-keras)) ### keras的一些參數和function 在做model.compile的時候 其中optimizer和loss可以自訂 例: ``` opt=Adam(lr=0.001) def my_loss_fn(y_true, y_pred): squared_difference = tf.abs(y_true - y_pred) return tf.reduce_mean(squared_difference, axis=-1) # Note the `axis=-1` model.compile(optimizer=opt, loss=my_loss_fn) ``` ### 關於loss = nan的幾種可能性 1. NN架構設計不良 2. 學習率太高 3. 資料中有nan 4. 對於回歸問題本身出現除以0的狀況 第3點很基本所以很容易忽略! 記得一定要檢查!!!(血的教訓) 參考資料:[为什么用tensorflow训练网络,出现了loss=nan,accuracy总是一个固定值?](https://www.zhihu.com/question/62441748)