6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめに

結論

  • 公式のExamplesはそのままイゴきました :rocket::rocket::rocket:
  • 予想通りといえば予想通りですが、パソコン上でうごいたらそのままNervesアプリとして、Raspberry Pi 2等でイゴくというのはいいですよね:bangbang::bangbang::bangbang:
    • な〜んにもかえなくて、そのままイゴきました
    • というか私自身はいまだにNervesアプリのほうで特殊なことをしないと動かなかったというものに出会ったことはありません

参考

環境構築

Nervesアプリの開発

Nxの導入

mix.exs
  defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
      {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", branch: "main", sparse: "nx"}
    ]
  end
end

  • 2021/2/20時点はこんな感じです
    • おそらく今後は、hexのほうにも登録されてもっと簡単な指定方法ですむようになるとおもいます
  • mix.exsを書き換えたらターミナルで以下のコマンドをうって、Nxを導入します
> export MIX_TARGET=rpi2
> mix deps.get
> mix firmware
> mix upload
  • 一気にファームウェアの更新までやっちゃいました
    • 最初の一回はmicroSDカードをパソコンにさして焼きこむ必要がありますが、以降はネットワーク越しにファームウェアを書き換えることができます
    • Nervesアプリ開発を続けているとこれがあたりまえになって、ついついそのありがたみを忘れがちなのですし、これを言葉で説明するのはなかなか難しいのですが、実際にやってみるとものすごく便利です:bangbang::bangbang::bangbang:
    • 普段の開発でも便利ですし、このへんの仕組みっていうのは、めぐりめぐってNervesHubにつながるものだとおもっています
      • NervesHub is an extensible web service that allows you to manage over-the-air (OTA) firmware updates of devices in the field. Built with Phoenix, NervesHub delivers first-class support for hardware deployments directly from the command line.
    • 遠く離れたところにあるデバイスのファームウェアをOTAで書き換えられるのってすごくないですか:bangbang::bangbang::bangbang:

Run

  • 私は現段階ではAI?、ML?、TensorFlow?の区別すらよくわかっていないのでとりあえず写しただけです

Examplesを写してみます

# ssh nerves-rpi2.local
  • IExで実行してみます1
iex(pi@nerves-rpi2.local)1> t = Nx.tensor([[1, 2], [3, 4]])
#Nx.Tensor<
  s64[2][2]
  [
    [1, 2],
    [3, 4]
  ]
>

iex(pi@nerves-rpi2.local)2> Nx.shape(t)
{2, 2}

iex(pi@nerves-rpi2.local)3> t = Nx.tensor([[1, 2], [3, 4]])
#Nx.Tensor<
  s64[2][2]
  [
    [1, 2],
    [3, 4]
  ]
>

iex(pi@nerves-rpi2.local)4> Nx.divide(Nx.exp(t), Nx.sum(Nx.exp(t)))
#Nx.Tensor<
  f64[2][2]
  [
    [0.03205860328008499, 0.08714431874203257],
    [0.23688281808991013, 0.6439142598879722]
  ]
>

iex(pi@nerves-rpi2.local)5> defmodule MyModule do
...(pi@nerves-rpi2.local)5>   import Nx.Defn
...(pi@nerves-rpi2.local)5> 
...(pi@nerves-rpi2.local)5>   defn softmax(t) do
...(pi@nerves-rpi2.local)5>     Nx.exp(t) / Nx.sum(Nx.exp(t))
...(pi@nerves-rpi2.local)5>   end
...(pi@nerves-rpi2.local)5> end
{:module, MyModule,
 <<70, 79, 82, 49, 0, 0, 10, 64, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 157,
   0, 0, 0, 36, 15, 69, 108, 105, 120, 105, 114, 46, 77, 121, 77, 111, 100, 117,
   108, 101, 8, 95, 95, 105, 110, 102, 111, ...>>, {:softmax, 1}}

iex(pi@nerves-rpi2.local)6> MyModule.softmax 1
#Nx.Tensor<
  f64
  1.0
>
  • $\huge{イゴいています!}$

九九

iex(pi@nerves-rpi2.local)12> s = Enum.map(1..9, &List.duplicate(&1, 9)) |> Nx.tensor()
#Nx.Tensor<
  s64[9][9]
  [
    [1, 1, 1, 1, 1, 1, 1, 1, 1],
    [2, 2, 2, 2, 2, 2, 2, 2, 2],
    [3, 3, 3, 3, 3, 3, 3, 3, 3],
    [4, 4, 4, 4, 4, 4, 4, 4, 4],
    [5, 5, 5, 5, 5, 5, 5, 5, 5],
    [6, 6, 6, 6, 6, 6, 6, 6, 6],
    [7, 7, 7, 7, 7, 7, 7, 7, 7],
    [8, 8, 8, 8, 8, 8, 8, 8, 8],
    [9, 9, 9, 9, 9, 9, 9, 9, 9]
  ]
>

iex(pi@nerves-rpi2.local)13> t = Nx.transpose(s)
#Nx.Tensor<
  s64[9][9]
  [
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
  ]
>

iex(pi@nerves-rpi2.local)14> Nx.multiply(s,t)
#Nx.Tensor<
  s64[9][9]
  [
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [2, 4, 6, 8, 10, 12, 14, 16, 18],
    [3, 6, 9, 12, 15, 18, 21, 24, 27],
    [4, 8, 12, 16, 20, 24, 28, 32, 36],
    [5, 10, 15, 20, 25, 30, 35, 40, 45],
    [6, 12, 18, 24, 30, 36, 42, 48, 54],
    [7, 14, 21, 28, 35, 42, 49, 56, 63],
    [8, 16, 24, 32, 40, 48, 56, 64, 72],
    [9, 18, 27, 36, 45, 54, 63, 72, 81]
  ]
>
  • $\huge{イゴいています!}$

Wrapping Up :lgtm::lgtm::lgtm::lgtm::lgtm:

  • とりあえず触ってみました
    • 触ってみただけです
    • 雰囲気すごそうな感じはしています
    • まだみなさんと語り合うレベルにはないので周辺知識とかをつけていきたいと強く感じました
    • $\huge{2021/02/25(木) 19:00〜}$
    • NervesJP #15 Nxを触ってみる回の予習ができました:bangbang:
  • Enjoy Elixir :rocket::rocket::rocket::rocket::rocket:

ありがとナイス:flag_cn:

れっつじょいなす(Let's join us) :bangbang::bangbang::bangbang:
:point_down::point_down_tone1::point_down_tone2::point_down_tone3::point_down_tone4::point_down_tone5:
NervesJP Slackへの参加URL
:point_up::point_up_tone1::point_up_tone2::point_up_tone3::point_up_tone4::point_up_tone5:

https___qiita-user-contents.imgix.net_https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F240349%2F5ef22bb9-f357-778c-1bff-b018cce54948.png_ixlib=rb-1.2.png


  1. config/target.exshostnameを指定しています。@nishiuchikazumaさんの「nerves.local の名前を orenonerves.local にする」を参考にしてください。 

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?