Using the TimeSeriesNnet() method from the nnet_ts

2020-07-30 03:52发布

问题:

I am trying to create a neural network using the python module nnet-ts. It has a built-in method named TimeSeriesNnet(), which takes two arguments; hidden_layers and activation_functions.

See documentation for this module, as well as example in the README.md:

https://github.com/hawk31/nnet-ts

I am running python version 2.7.13

The nnet-ts module has dependencies to 5 particular packages, which I am listing below together with the current versions I am using:

numpy-1.13.0, pandas-0.20.2, scipy-0.19.0, theano-0.9.0 and keras-2.0.5

Following the example in the README (link above), my code reads as follows:

from nnet_ts import *
neural_net = TimeSeriesNnet(hidden_layers = [7, 3], activation_functions = ['tanh', 'tanh'])

Execution of this code results in a NameError being thrown. Output:

NameError: name 'TimeSeriesNnet' is not defined

My guess is that the reason for this error could have something to do with different module versions, as the code is nearly identical to the example given in the README. Any help is much appreciated.

回答1:

Definitely, there is something wrong with your deployment of the code. For your Python developments, I suggest always use a local virtual environment. For this Neural Network Timeseries, you can build it locally with the python setup.py build command, it will build necessary files in the build/lib.linux-x86_64-2.7 directory. Assuming you have all dependencies, there shouldn't be any problem. In the deployment, you just have to set your PYTHONPATH to this directory, or you just include this path in the runtime with sys.path.insert() method.
For example, assuming I am in nnet-ts directory, I have built and run nnet-ts as follow:

    $ python setup.py build
    $ python
    > import sys
    > sys.path.insert(0, 'build/lib.linux-x86_64-2.7')
    > from nnet_ts import *
    > time_series = np.array(pd.read_csv("nnet_ts/AirPassengers.csv")["x"]) 
    > neural_net = TimeSeriesNnet(hidden_layers = [20, 15, 5], activation_functions = ['sigmoid', 'sigmoid', 'sigmoid'])