Hang123_
Hang123_
2020-04-18 21:56
采纳率: 100%
浏览 634

lstm自编码器代码,看不懂,求大神指导

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Learning LSTM Autoencoder and LSTM Network on a simple Multivariate Timeseries Toy example"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# lstm autoencoder to recreate a timeseries\n",
"import numpy as np\n",
"from keras.models import Sequential\n",
"from keras.layers import LSTM\n",
"from keras.layers import Dense\n",
"from keras.layers import RepeatVector\n",
"from keras.layers import TimeDistributed"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"'''\n",
"A UDF to convert input data into 3-D\n",
"array as required for LSTM network.\n",
"'''\n",
"\n",
"def temporalize(X, y, lookback):\n",
" output_X = []\n",
" output_y = []\n",
" for i in range(len(X)-lookback-1):\n",
" t = []\n",
" for j in range(1,lookback+1):\n",
" # Gather past records upto the lookback period\n",
" t.append(X[[(i+j+1)], :])\n",
" output_X.append(t)\n",
" output_y.append(y[i+lookback+1])\n",
" return output_X, output_y"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.1 , 0.001],\n",
" [0.2 , 0.008],\n",
" [0.3 , 0.027],\n",
" [0.4 , 0.064],\n",
" [0.5 , 0.125],\n",
" [0.6 , 0.216],\n",
" [0.7 , 0.343],\n",
" [0.8 , 0.512],\n",
" [0.9 , 0.729]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# define input timeseries\n",
"timeseries = np.array([[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],\n",
" [0.1**3, 0.2**3, 0.3**3, 0.4**3, 0.5**3, 0.6**3, 0.7**3, 0.8**3, 0.9**3]]).transpose()\n",
"\n",
"timesteps = timeseries.shape[0]\n",
"n_features = timeseries.shape[1]\n",
"timeseries"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0.3 , 0.027],\n",
" [0.4 , 0.064],\n",
" [0.5 , 0.125]],\n",
"\n",
" [[0.4 , 0.064],\n",
" [0.5 , 0.125],\n",
" [0.6 , 0.216]],\n",
"\n",
" [[0.5 , 0.125],\n",
" [0.6 , 0.216],\n",
" [0.7 , 0.343]],\n",
"\n",
" [[0.6 , 0.216],\n",
" [0.7 , 0.343],\n",
" [0.8 , 0.512]],\n",
"\n",
" [[0.7 , 0.343],\n",
" [0.8 , 0.512],\n",
" [0.9 , 0.729]]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"timesteps = 3\n",
"X, y = temporalize(X = timeseries, y = np.zeros(len(timeseries)), lookback = timesteps)\n",
"\n",
"n_features = 2\n",
"X = np.array(X)\n",
"X = X.reshape(X.shape[0], timesteps, n_features)\n",
"\n",
"X"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## LSTM Autoencoder"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"lstm_5 (LSTM) (None, 3, 128) 67072 \n",
"_________________________________________________________________\n",
"lstm_6 (LSTM) (None, 64) 49408 \n",
"_________________________________________________________________\n",
"repeat_vector_1 (RepeatVecto (None, 3, 64) 0 \n",
"_________________________________________________________________\n",
"lstm_7 (LSTM) (None, 3, 64) 33024 \n",
"_________________________________________________________________\n",
"lstm_8 (LSTM) (None, 3, 128) 98816 \n",
"_________________________________________________________________\n",
"time_distributed_2 (TimeDist (None, 3, 2) 258 \n",
"=================================================================\n",
"Total params: 248,578\n",
"Trainable params: 248,578\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# define model\n",
"model = Sequential()\n",
"model.add(LSTM(128, activation='relu', input_shape=(timesteps,n_features), return_sequences=True))\n",
"model.add(LSTM(64, activation='relu', return_sequences=False))\n",
"model.add(RepeatVector(timesteps))\n",
"model.add(LSTM(64, activation='relu', return_sequences=True))\n",
"model.add(LSTM(128, activation='relu', return_sequences=True))\n",
"model.add(TimeDistributed(Dense(n_features)))\n",
"model.compile(optimizer='adam', loss='mse')\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From /home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.cast instead.\n",
"---Predicted---\n",
"[[[0.323 0.041]\n",
" [0.423 0.069]\n",
" [0.494 0.121]]\n",
"\n",
" [[0.391 0.069]\n",
" [0.499 0.126]\n",
" [0.587 0.209]]\n",
"\n",
" [[0.491 0.119]\n",
" [0.596 0.216]\n",
" [0.699 0.344]]\n",
"\n",
" [[0.596 0.203]\n",
" [0.693 0.34 ]\n",
" [0.808 0.513]]\n",
"\n",
" [[0.701 0.347]\n",
" [0.798 0.509]\n",
" [0.892 0.723]]]\n",
"---Actual---\n",
"[[[0.3 0.027]\n",
" [0.4 0.064]\n",
" [0.5 0.125]]\n",
"\n",
" [[0.4 0.064]\n",
" [0.5 0.125]\n",
" [0.6 0.216]]\n",
"\n",
" [[0.5 0.125]\n",
" [0.6 0.216]\n",
" [0.7 0.343]]\n",
"\n",
" [[0.6 0.216]\n",
" [0.7 0.343]\n",
" [0.8 0.512]]\n",
"\n",
" [[0.7 0.343]\n",
" [0.8 0.512]\n",
" [0.9 0.729]]]\n"
]
}
],
"source": [
"# fit model\n",
"model.fit(X, X, epochs=300, batch_size=5, verbose=0)\n",
"# demonstrate reconstruction\n",
"yhat = model.predict(X, verbose=0)\n",
"print('---Predicted---')\n",
"print(np.round(yhat,3))\n",
"print('---Actual---')\n",
"print(np.round(X, 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Regular LSTM Network"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"lstm_9 (LSTM) (None, 3, 128) 67072 \n",
"_________________________________________________________________\n",
"lstm_10 (LSTM) (None, 3, 64) 49408 \n",
"_________________________________________________________________\n",
"lstm_11 (LSTM) (None, 3, 64) 33024 \n",
"_________________________________________________________________\n",
"lstm_12 (LSTM) (None, 3, 128) 98816 \n",
"_________________________________________________________________\n",
"time_distributed_3 (TimeDist (None, 3, 2) 258 \n",
"=================================================================\n",
"Total params: 248,578\n",
"Trainable params: 248,578\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# define model\n",
"model = Sequential()\n",
"model.add(LSTM(128, activation='relu', input_shape=(timesteps,n_features), return_sequences=True))\n",
"model.add(LSTM(64, activation='relu', return_sequences=True))\n",
"model.add(LSTM(64, activation='relu', return_sequences=True))\n",
"model.add(LSTM(128, activation='relu', return_sequences=True))\n",
"model.add(TimeDistributed(Dense(n_features)))\n",
"model.compile(optimizer='adam', loss='mse')\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---Predicted---\n",
"[[[0.306 0.026]\n",
" [0.399 0.064]\n",
" [0.5 0.124]]\n",
"\n",
" [[0.393 0.064]\n",
" [0.502 0.124]\n",
" [0.599 0.215]]\n",
"\n",
" [[0.502 0.126]\n",
" [0.6 0.214]\n",
" [0.7 0.343]]\n",
"\n",
" [[0.596 0.219]\n",
" [0.699 0.344]\n",
" [0.798 0.51 ]]\n",
"\n",
" [[0.703 0.34 ]\n",
" [0.8 0.512]\n",
" [0.899 0.727]]]\n",
"---Actual---\n",
"[[[0.3 0.027]\n",
" [0.4 0.064]\n",
" [0.5 0.125]]\n",
"\n",
" [[0.4 0.064]\n",
" [0.5 0.125]\n",
" [0.6 0.216]]\n",
"\n",
" [[0.5 0.125]\n",
" [0.6 0.216]\n",
" [0.7 0.343]]\n",
"\n",
" [[0.6 0.216]\n",
" [0.7 0.343]\n",
" [0.8 0.512]]\n",
"\n",
" [[0.7 0.343]\n",
" [0.8 0.512]\n",
" [0.9 0.729]]]\n"
]
}
],
"source": [
"# fit model\n",
"model.fit(X, X, epochs=300, batch_size=5, verbose=0)\n",
"# demonstrate reconstruction\n",
"yhat = model.predict(X, verbose=0)\n",
"print('---Predicted---')\n",
"print(np.round(yhat,3))\n",
"print('---Actual---')\n",
"print(np.round(X, 3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • caozhy
    已采纳

    你这个不是python代码,而是json
    你试试看,把文件后缀修改为 ipynb
    用 jupyter notebook 打开

    点赞 评论

相关推荐