Commit 39ad30a0 authored by nitinprakash96's avatar nitinprakash96
Browse files

demonstrate tensorgraph layer constructions

parent a67cc572
Loading
Loading
Loading
Loading
+143 −0
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

### TensorGraph Layers and TensorFlow eager

%% Cell type:markdown id: tags:

 In this tutorial we will look at the working of TensorGraph layer with TensorFlow eager.

%% Cell type:code id: tags:

``` python
import tensorflow as tf
import tensorflow.contrib.eager as tfe
```

%% Output

    /home/nitin/anaconda3/envs/deepchem/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
      from ._conv import register_converters as _register_converters

%% Cell type:markdown id: tags:

After importing neccessary modules, at the program startup we invoke `enable_eager_execution()`.

%% Cell type:code id: tags:

``` python
tfe.enable_eager_execution()
```

%% Cell type:markdown id: tags:

### Importing TensorGraph Layers

%% Cell type:code id: tags:

``` python
import deepchem as dc
from deepchem.models.tensorgraph.layers import Dense, Conv1D
```

%% Cell type:code id: tags:

``` python
# Initialize a tensor
inputs = tf.constant([[1.0, 2.0], [4.0, 5.0]])
```

%% Cell type:code id: tags:

``` python
# This will create a Dense layer
dense_layer = Dense(3)
```

%% Cell type:markdown id: tags:

The `create_tensor()` function doesn't perform any tensor operation but is a method of `Dense`. It takes in a tensor object as a parameter.

%% Cell type:code id: tags:

``` python
x = dense_layer.create_tensor(in_layers = [inputs])
print(x)
```

%% Output

    tf.Tensor(
    [[-0.22144215  0.17204109 -0.91569924]
     [-1.1559541   0.81352365 -0.9357872 ]], shape=(2, 3), dtype=float32)

%% Cell type:markdown id: tags:

The above function call performs the same action as the below. This is because `create_tensor()` is invoked by `__call__()` object. This gives us an advantage of directly passing the tensor as a parameter while constructing a TensorGraph layer.

%% Cell type:code id: tags:

``` python
y = Dense(2)(inputs) # creates a layer that outputs a tensor of shape=(2x2)
print(y)
```

%% Output

    tf.Tensor(
    [[  1.2503337  -4.140855 ]
     [  2.821608  -10.878647 ]], shape=(2, 2), dtype=float32)

%% Cell type:markdown id: tags:

### Example

%% Cell type:markdown id: tags:

The following code snippet shows a basic architechture of how TensorGraph layer can be created in TensorFlow eager mode.

%% Cell type:markdown id: tags:

Import all the necessary modules and `enable_eager_execution()`.

```python
import tensorflow as tf
import tensorflow.contrib.eager as tfe
import deepchem as dc
from deepchem.models.tensorgraph.layers import Dense

tfe.enable_eager_execution()

x = [[4.]]
output = Dense(3)(x)
print(output)
```

%% Cell type:markdown id: tags:

`Dense` layers are one of the layers defined in Deepchem. Along with it there are several others like `Conv1D`, `Conv2D`, `conv3D` etc. We show constructing a `Conv1D` layer below.

%% Cell type:markdown id: tags:

Basically this layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs.

%% Cell type:code id: tags:

``` python
conv_layer = Conv1D(2, 1)
z = conv_layer.create_tensor(in_layers = [inputs])
print(z)
```

%% Output

    tf.Tensor(
    [[[0.53595114 0.15551412]
      [1.0719023  0.31102824]]
    
     [[2.1438046  0.6220565 ]
      [2.6797557  0.7775706 ]]], shape=(2, 2, 2), dtype=float32)

%% Cell type:markdown id: tags:

### Gradients

%% Cell type:code id: tags:

``` python
```