Rails5(Turbolinks5)でGoogle Analyticsを設置
Rails5(Turbolinks5)の環境でGoogle Analyticsを設置した時のメモ。
Turbolinks Compatibilityに掲載されている情報に古さが出てきていたので他の方法を調べていたところ、turbolinksのissuesで同じテーマのやりとりを見つけたのでこちらを参考にしました。
参考:Google analytics best practice? #73
<head> ... <% if Rails.env.production? %> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga("create", "UA-XXXXX-Y", "auto"); </script> <% end %> </head>
document.addEventListener "turbolinks:load", (event) -> if typeof ga is "function" ga("set", "location", event.data.url) ga("send", "pageview")
AdSenseのissueは下記のリンクに上がっています。
参考
DEPRECATION WARNING: alias_method_chain is deprecated
Rails5で作業中に下記のWARNINGが表示されました。
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super.
alias_method_chainが使用されているgemを調べます。
まずgemのインストール先を確認。
$ gem environment
インストール先のディレクトリに移動してalias_method_chainでgrepをかける。
$ grep -lir alias_method_chain ./
該当するgemを発見。
githubのリポジトリでchangelogを見たらfixされていたのでgemをアップデート。
WARNINGが表示されなくなったことを確認できました。
参考
Amazon EC2にSFTPで接続
Amazon EC2に「FTPで接続したい」と言われた時のメモ。
作業ユーザーを作成
$ sudo useradd hoge $ sudo passwd hoge
$ sudo visudo
hoge ALL=(ALL) ALL
鍵の作成
$ sudo su - hoge $ mkdir ~/.ssh $ ssh-keygen -t rsa $ mv id_rsa.pub authorized_keys $ chmod 600 authorized_keys $ chmod 700 ~/.ssh
秘密鍵を適当な名前でローカルに保存。
確認
接続を確認
ssh -i hoge.pem hoge@xx.xxx.xx.xx
書き込み権限を許可
sudo chmod o+w /var/www/html/huga
その他
エラー:Permissions 0644 for ‘hoge.pem’ are too open.
$ chmod 0600 hoge.pem
参考
NameError: uninitialized constant AWS
「NameError: uninitialized constant AWS」のエラーが出た時の対応メモ。
エラー内容
NameError: uninitialized constant AWS
原因
NameError: uninitialized constant AWS. If you receive this error, you likely have upgraded to version 2 of the aws-sdk gem unintentionally. Version 2 uses the Aws namespace, not AWS. This allows version 1 and version 2 to be used in the same application.
参考:https://github.com/aws/aws-sdk-ruby#nameerror-uninitialized-constant-aws
対応
ネームスペースの修正。
version 1(修正前)
AWS.config({ :access_key_id => ENV["AWS_ACCESS_KEY_ID"], :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"] })
version 2(修正後)
Aws.config.update({ region: 'us-west-2', credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']) })
リージョンの指定は適宜。
参考
Rails環境の構築
Rails環境構築
rbenv + ruby-build
$ sudo yum update $ sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison git $ git clone https://github.com/rbenv/rbenv.git ~/.rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile $ source ~/.bash_profile $ rbenv -v $ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build $ rbenv install -l $ rbenv install 2.2.2 $ rbenv global 2.2.2 $ rbenv rehash $ rbenv versions
Rails
$ gem install rails --version="5.0.1" $ rails -v
Nginx
$ sudo yum -y install nginx $ sudo service nginx start $ sudo chkconfig nginx on $ sudo chkconfig --list nginx
Basic認証
参考
ChainerでMNISTを試す
ChainerでMNISTのサンプルコードを試しました。
Chainerをインストール
$ pip install chainer
サンプルコードを動かす
chainer/examples/mnistのディレクトリにMNISTのサンプルコードがあります。学習時間は15分程度でした。
$ python train_ptb.py GPU: -1 # unit: 1000 # Minibatch-size: 100 # epoch: 20 Downloading from http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz... Downloading from http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz... Downloading from http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz... Downloading from http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz... epoch main/loss validation/main/loss main/accuracy validation/main/accuracy elapsed_time 1 0.191542 0.090873 0.944033 0.9713 50.9736 2 0.0741329 0.0754302 0.976317 0.9765 99.1206 3 0.0467661 0.0721968 0.985067 0.9797 161.049 4 0.0359696 0.0728168 0.988083 0.978 209.989 5 0.0290916 0.0821801 0.9904 0.977 261.491 6 0.0242685 0.0734186 0.9921 0.9806 311.869 7 0.0202083 0.0895192 0.993467 0.9795 360.346 8 0.0173684 0.0807823 0.9944 0.9785 407.156 9 0.0185823 0.0856489 0.994 0.9792 456.174 10 0.0167096 0.0783728 0.9947 0.9797 506.884 11 0.0103569 0.095445 0.9968 0.9797 556.816 12 0.0153957 0.0862309 0.995267 0.982 605.577 13 0.00907149 0.104247 0.9972 0.9803 653.676 14 0.013558 0.0988746 0.995767 0.9794 708.906 15 0.0123224 0.0865951 0.99605 0.9825 762.177 16 0.0102555 0.0927854 0.99725 0.982 815.14 17 0.0100652 0.094853 0.996733 0.9805 877.995 18 0.00979173 0.0833932 0.997183 0.9849 932.327 19 0.00768291 0.116505 0.997767 0.9801 992.164 20 0.00946933 0.100418 0.9974 0.9836 1054.31
学習が終わるとresultディレクトリの中に学習結果のデータが作成されます。
accuracyが精度、lossが誤差を表しています。学習回数が増す毎に数値が改善されています。(グラフの読み方は勉強中です。)
まとめ
MNISTのサンプルコードを試すことができました。このあとは、コードの中身をチラ見したり、他に用意されているサンプルを試しながら、少しずつ自分でもコードを書けるようになればと思います。