How to cast with Theano

Cast in Theano

theano is a very strict typed library.

There is no powerful type inference like python, so of course you will want to take advantage of the cast.

I will cast it for the time being

The cast of theano is implemented by theano.tensor.cast ().

Let's use cast immediately. By default, a matrix defined in float64 type is converted to int32 type.

python


import theano.tensor as T
x = T.matrix()
x_as_int = T.cast(x, 'int32')

type(x), type(x_as_int)

Output result


(theano.tensor.var.TensorVariable, theano.tensor.var.TensorVariable)

Isn't it cast?

In the above results, both show the same type (TensorVariable).

This is because Theano's specification defines symbols as variables, not values.

Use theano.printing.debugprint () to see the type of the symbol's contents.

type of x


import theano
theano.printing.debugprint(x)

Output result


<TensorType(float64, matrix)> [id A]

x_as_int type


theano.printing.debugprint(x_as_int)

Output result


Elemwise{Cast{int32}} [id A] ''   
 |<TensorType(float64, matrix)> [id B]

I see, it was originally a float64 type, but in x_as_int it seems to be cast to int32.

Let's actually enter the value and check the behavior.

Check type through function

To put a value in a symbol, you need to define a function.

By defining a formula using symbols as a function and entering a value in that function, the processing result of the formula is returned as the return value of the function.

At this time, the type of the input value depends on the symbol type.

If you put in an inappropriate type, you will get an error, so

If you set the int32 symbol x_as_int as input, you should get an error if you enter a decimal.

Function definition


import numpy as np

mat = np.array([[1.0, 0.0], [0.0, 1.0]], dtype="float64")
mat_int = np.array([[1, 0], [0, 1]], dtype="int32")

y = x * 2
f = theano.function(inputs=[x], outputs=y)

y_as_int = x_as_int * 2
f_as_int = theano.function(inputs=[x_as_int], outputs=y_as_int)

f(x)


f(mat)

Execution result


array([[ 2.,  0.],
       [ 0.,  2.]])

f(x_as_int)


f(mat_int)

Execution result


array([[ 2.,  0.],
       [ 0.,  2.]])

f_as_int(x)


f_as_int(mat)

Execution result



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-31692f0163e9> in <module>()
----> 1 f_as_int(mat)

/home/ubuntu/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    786                         s.storage[0] = s.type.filter(
    787                             arg, strict=s.strict,
--> 788                             allow_downcast=s.allow_downcast)
    789 
    790                     except Exception as e:

/home/ubuntu/anaconda3/lib/python3.5/site-packages/theano/tensor/type.py in filter(self, data, strict, allow_downcast)
    138                             '"function".'
    139                             % (self, data.dtype, self.dtype))
--> 140                         raise TypeError(err_msg, data)
    141                 elif (allow_downcast is None and
    142                         type(data) is float and

TypeError: ('Bad input argument to theano function with name "<ipython-input-54-50af382d0dd4>:2" at index 0 (0-based)', 'TensorType(int32, matrix) cannot store a value of dtype float64 without risking loss of precision. If you do not mind this loss, you can: 1) explicitly cast your data to int32, or 2) set "allow_input_downcast=True" when calling "function".', array([[ 1.,  0.],
       [ 0.,  1.]]))

f_as_int(x_as_int)


array([[2, 0],
       [0, 2]], dtype=int32)

As expected, f_as_int (mat) with the input value set to float64 type while specifying the int32 type as the input symbol threw an error.

Now you can see the cast movement in theano safely.

Recommended Posts

How to cast with Theano
How to update with SQLAlchemy?
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to cancel RT with tweepy
Python: How to use async with
How to use classes in Theano
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to get started with Scrapy
How to deal with DistributionNotFound errors
How to get started with Django
How to Data Augmentation with PyTorch
How to use FTP with Python
How to calculate date with python
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to install Theano on Mac OS X with homebrew
How to do arithmetic with Django template
[Blender] How to set shape_key with script
How to title multiple figures with matplotlib
How to get parent id with sqlalchemy
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to prevent package updates with apt
How to work with BigQuery in Python
How to use Ass / Alembic with HtoA
How to deal with enum compatibility errors
How to use Japanese with NLTK plot
How to do portmanteau test with python
How to search Google Drive with Google Colaboratory
How to display python Japanese with lolipop
How to download youtube videos with youtube-dl
How to use jupyter notebook with ABCI
How to power off Linux with Ultra96-V2
"How to pass PATH" to learn with homebrew
How to scrape websites created with SPA
How to use CUT command (with sample)
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to install zsh (with .zshrc customization)
How to read problem data with paiza
How to use SQLAlchemy / Connect with aiomysql
How to get started with laravel (Linux)
How to group volumes together with LVM
How to install python3 with docker centos
How to use JDBC driver with Redash
Cast with python
How to upload with Heroku, Flask, Python, Git (4)
How to deal with memory leaks in matplotlib.pyplot
How to create sample CSV data with hypothesis
How to read a CSV file with Python 2/3
How to use GCP trace with open Telemetry
How to reduce GPU memory usage with Keras