fc2ブログ

記事一覧

Theano を使ってみる

いろいろやりたいことが多く迷走している気がする。
ひとまずやってみたら見えてくることもあるだろうと楽観的に考えてやってみる。

こちら の連載を参考に Theano を使ってみます。

環境構築

Theanoインストール

>git clone git://github.com/Theano/Theano.git
Cloning into 'Theano'...
...
Checking out files: 100% (833/833), done.

> cd Theano
> python setup.py install
running install
...
Finished processing dependencies for Theano==1.0.4+12.g93e8180bf


importでwarningが出たが、とりあえあず動いているので、いろいろ試している中でダメそうなら解決することとする

> import theano
WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
C:\temp\Theano\theano\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory
warnings.warn("DeprecationWarning: there is no c++ compiler."
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.

> import theano.tensor as T
> x = T.dscalar('x')
> print(type(x))
< class 'theano.tensor.var.TensorVariable'>
> y = x ** 2
> print(type(y))
< class 'theano.tensor.var.TensorVariable'>
> f = theano.function(inputs=[x], outputs=y)
> print(type(f))
< class 'theano.compile.function_module.Function'>
> print(f(1))
1.0
> print(f(2))
4.0


数式演算

こちら で書いていたfeedforwardの演算は以下のようになるのだろうか

> import numpy as np
> import theano
> import theano.tensor as T
> W = theano.shared(np.array(np.random.rand(3, 3), dtype=theano.config.floatX), name='W', borrow=True)
> b = theano.shared(np.array(np.random.rand(1, 3), dtype=theano.config.floatX), name='b', borrow=True)
> print(W.get_value())
[[0.6517933 0.46303742 0.87898708]
[0.03661336 0.9037741 0.65230641]
[0.24282337 0.39399875 0.01552468]]
> print(b.get_value())
[[0.71017407 0.39113233 0.26412777]]
> x = T.vector('x')
> y = T.dot(W, x) + b
> print(type(y))
< class 'theano.tensor.var.TensorVariable'>
> f = theano.function([x], y)
> print(f([0, 0, 1]))
[[1.58916115 1.04343875 0.27965246]]


(2019/8/23 追記)

import theano で、以下のエラーが出るようになった

>>> import theano

You can find the C code in this temporary file: C:\Users\user\AppData\Local\Temp\theano_compilation_error_alqzk_ao
Traceback (most recent call last):
File "c:\work\Theano\theano\gof\lazylinker_c.py", line 81, in
actual_version, force_compile, _need_reload))
ImportError: Version check of the existing lazylinker compiled file. Looking for version 0.211, but found None. Extra debug information: force_compile=False, _need_reload=True

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\work\Theano\theano\gof\lazylinker_c.py", line 105, in
actual_version, force_compile, _need_reload))
ImportError: Version check of the existing lazylinker compiled file. Looking for version 0.211, but found None. Extra debug information: force_compile=False, _need_reload=True

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 1, in
File "c:\work\Theano\theano\__init__.py", line 110, in
from theano.compile import (
File "c:\work\Theano\theano\compile\__init__.py", line 12, in
from theano.compile.mode import *
File "c:\work\Theano\theano\compile\mode.py", line 11, in
import theano.gof.vm
File "c:\work\Theano\theano\gof\vm.py", line 674, in
from . import lazylinker_c
File "c:\work\Theano\theano\gof\lazylinker_c.py", line 140, in
preargs=args)
File "c:\work\Theano\theano\gof\cmodule.py", line 2396, in compile_str
(status, compile_stderr.replace('\n', '. ')))
Exception: Compilation failed (return status=1): cc1plus.exe: sorry, unimplemented: 64-bit mode not compiled in.


以下を参考に解決
https://qiita.com/jyori112/items/766563384d6a9d410212

参考ページと同じく、Targetがmingw32になっていたので再度インストールしたが、Targetは変わらずエラーも解消しなかった。

>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/8.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-8.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-8.2.0-3' --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-isl=/mingw --enable-libgomp --disable-libvtv --enable-nls --disable-build-format-warnings
Thread model: win32
gcc version 8.2.0 (MinGW.org GCC-8.2.0-3)


というかMinGWをアンインストール(C:\MinGWを削除)したらエラー出なくなった
スポンサーサイト



コメント

コメントの投稿

非公開コメント