[PYTHON] How to use classes in Theano

How to use classes in Theano

Deeplearning.net etc. use Theano to define classes that have shared variables and expression symbols as class variables.

Let's check the behavior of this class.

Here, simply define a class with one shared variable and one expression symbol, and try to utilize the class variable.

python


import numpy as np
import theano
import theano.tensor as T

class simple(object):
    
    def __init__(self, input):
        
        self.vector = theano.shared(np.array([1, 2, 3], dtype="float64"))
        
        self.formula = self.vector * input

x = T.iscalar('x')

s = simple(x)

func = theano.function(inputs=[x], outputs=s.formula)

func(5)

Output result


array([  5.,  10.,  15.])

The behavior after defining the class is a little complicated, so let's summarize it.

  1. Define the symbol of the variable
  2. Create an instance. Takes the symbol as an argument at initialization and passes it to the expression symbol of the class variable
  3. Call the expression symbol of the class (instance) variable to define the function
  4. Execute the function

Is it the point or the cause of complexity that the description in the class includes the expression symbol?

Expression symbols cannot be called directly to check their values.

It is also difficult to understand the behavior unless you make it a function and enter a concrete value.

You can use Theano's classes in this way.

Recommended Posts

How to use classes in Theano
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use calculated columns in CASTable
How to use Google Test in C
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use is and == in Python
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use the C library in Python
Study from Python Hour7: How to use classes
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to use tkinter with python in pyenv
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv