# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <h1> Time series prediction, end-to-end </h1>
#
# This notebook illustrates several models to find the next value of a time-series:
# <ol>
# <li> Linear
# <li> DNN
# <li> CNN 
# <li> RNN
# </ol>

PROJECT = "qwiklabs-gcp-636667ae83e902b6"  # Replace with your PROJECT
BUCKET =  "qwiklabs-gcp-636667ae83e902b6_al"  # Replace with your BUCKET
REGION = "us-east1"            # Choose an available region for AI Platform  
TFVERSION = "1.13"                # TF version for AI Platform
SEQ_LEN = 50

import os
os.environ['BUCKET'] = BUCKET
os.environ['PROJECT'] = PROJECT
os.environ['REGION'] = REGION
os.environ['SEQ_LEN'] = str(SEQ_LEN)
os.environ['TFVERSION'] = '1.13'

# <h3> Simulate some time-series data </h3>
#
# Essentially a set of sinusoids with random amplitudes and frequencies.

import tensorflow as tf
print(tf.__version__)

# +
import numpy as np
import seaborn as sns

def create_time_series():
  freq = (np.random.random()*0.5) + 0.1  # 0.1 to 0.6
  ampl = np.random.random() + 0.5  # 0.5 to 1.5
  noise = [np.random.random()*0.3 for i in range(SEQ_LEN)] # -0.3 to +0.3 uniformly distributed
  x = np.sin(np.arange(0,SEQ_LEN) * freq) * ampl + noise
  return x

flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
for i in range(0, 5):
  sns.tsplot( create_time_series(), color=flatui[i%len(flatui)] );  # 5 series


# +
def to_csv(filename, N):
  with open(filename, 'w') as ofp:
    for lineno in range(0, N):
      seq = create_time_series()
      line = ",".join(map(str, seq))
      ofp.write(line + '\n')

import os
try:
  os.makedirs('data/sines/')
except OSError:
  pass
to_csv('data/sines/train-1.csv', 1000)  # 1000 sequences
to_csv('data/sines/valid-1.csv', 250)
# -

# !head -5 data/sines/*-1.csv

# <h3> Train model locally </h3>
#
# Make sure the code works as intended.
#
# The `model.py` and `task.py` containing the model code is in <a href="sinemodel">sinemodel/</a>
#
# **Complete the TODOs in `model.py` before proceeding!**
#
# Once you've completed the TODOs, set `--model` below to the appropriate model (linear,dnn,cnn,rnn,rnn2 or rnnN) and run it locally for a few steps to test the code.

# + language="bash"
# DATADIR=$(pwd)/data/sines
# OUTDIR=$(pwd)/trained/sines
# rm -rf $OUTDIR
# gcloud ai-platform local train \
#    --module-name=sinemodel.task \
#    --package-path=${PWD}/sinemodel \
#    -- \
#    --train_data_path="${DATADIR}/train-1.csv" \
#    --eval_data_path="${DATADIR}/valid-1.csv"  \
#    --output_dir=${OUTDIR} \
#    --model=rnn --train_steps=1000 --sequence_length=$SEQ_LEN
# -

# Ten steps:
# * Linear: RMSE = 0.86
# * DNN: RMSE = 0.44
# * RNN: RMSE = 0.46
#
# 1000 steps:
# * Linear:
# * DNN: 
# * RNN: 
#
#

# <h3> Cloud ML Engine </h3>
#
# Now to train on Cloud ML Engine with more data.

import shutil
shutil.rmtree('data/sines', ignore_errors=True)
os.makedirs('data/sines/')
for i in range(0,10):
  to_csv('data/sines/train-{}.csv'.format(i), 1000)  # 1000 sequences
  to_csv('data/sines/valid-{}.csv'.format(i), 250)

# + language="bash"
# gsutil -m rm -rf gs://${BUCKET}/sines/*
# gsutil -m cp data/sines/*.csv gs://${BUCKET}/sines

# + language="bash"
# for MODEL in linear dnn cnn rnn rnn2 rnnN; do
#   OUTDIR=gs://${BUCKET}/sinewaves/${MODEL}
#   JOBNAME=sines_${MODEL}_$(date -u +%y%m%d_%H%M%S)
#   gsutil -m rm -rf $OUTDIR
#   gcloud ml-engine jobs submit training $JOBNAME \
#      --region=$REGION \
#      --module-name=sinemodel.task \
#      --package-path=${PWD}/sinemodel \
#      --job-dir=$OUTDIR \
#      --staging-bucket=gs://$BUCKET \
#      --scale-tier=BASIC_GPU \
#      --runtime-version=$TFVERSION \
#      -- \
#      --train_data_path="gs://${BUCKET}/sines/train*.csv" \
#      --eval_data_path="gs://${BUCKET}/sines/valid*.csv"  \
#      --output_dir=$OUTDIR \
#      --train_steps=3000 --sequence_length=$SEQ_LEN --model=$MODEL
# done
# -

# ## Monitor training with TensorBoard
#
# Use this cell to launch tensorboard. If tensorboard appears blank try refreshing after 5 minutes

from google.datalab.ml import TensorBoard
TensorBoard().start('gs://{}/sinewaves'.format(BUCKET))

for pid in TensorBoard.list()['pid']:
  TensorBoard().stop(pid)
  print('Stopped TensorBoard with pid {}'.format(pid))

# ## Results
#
# Complete the below table with your own results! Then compare your results to the results in the solution notebook.
#
# | Model | Sequence length | # of steps | Minutes | RMSE |
# | --- | ----| --- | --- | --- | 
# | linear | 50 | 3000 | - | - |
# | dnn | 50 | 3000 | - | - |
# | cnn | 50 | 3000 | - | - |
# | rnn | 50 | 3000 | - | - |
# | rnn2 | 50 | 3000 | - | - |
# | rnnN | 50 | 3000 | - | - |

# Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License

StarcoderdataJupyterScriptsDedupFiltered