File: sharedvar.py

package info (click to toggle)
theano 1.0.3+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 30,752 kB
  • sloc: python: 141,182; ansic: 9,505; makefile: 259; sh: 214; pascal: 81
file content (32 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ import absolute_import, print_function, division
import copy
import scipy.sparse
from theano.compile import shared_constructor, SharedVariable
from theano.sparse.basic import SparseType, _sparse_py_operators


class SparseTensorSharedVariable(_sparse_py_operators, SharedVariable):
    dtype = property(lambda self: self.type.dtype)
    format = property(lambda self: self.type.format)


@shared_constructor
def sparse_constructor(value, name=None, strict=False, allow_downcast=None,
                       borrow=False, format=None):
    """
    SharedVariable Constructor for SparseType.

    writeme

    """
    if not isinstance(value, scipy.sparse.spmatrix):
        raise TypeError("Expected a sparse matrix in the sparse shared variable constructor. Received: ",
                        value.__class__)

    if format is None:
        format = value.format
    type = SparseType(format=format, dtype=value.dtype)
    if not borrow:
        value = copy.deepcopy(value)
    return SparseTensorSharedVariable(type=type, value=value, name=name,
                                      strict=strict, allow_downcast=allow_downcast)