在一行中建立神经网络
nn-builder的Python项目详细描述
nn_builder允许您在不使用样板代码的情况下构建神经网络。您可以指定所需的网络类型并由它构建。
安装
pip install nn_builder
支持
Network Type | NN | CNN | RNN |
---|---|---|---|
PyTorch | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
TensorFlow 2.0 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
示例
左边是如何使用nn_builder在右边的一行代码中创建pytorch神经网络:
类似地,对于左侧的tensorflow,您可以使用nn_builder在右侧仅用一行代码创建cnn:
使用量
有关如何使用该模块的许多示例,请参见colab notebook。 目前支持三种类型的pytorch和tensorflow网络:nn、cnn和rnn。每个网络采用以下参数:
Field | Description | Default |
---|---|---|
input_dim | Dimension of the input into the network. See below for more detail. Not needed for Tensorflow. | N/A |
layers_info | List to indicate the layers of the network you want. Exact requirements depend on network type, see below for more detail | N/A |
output_activation | String to indicate the activation function you want the output to go through. Provide a list of strings if you want multiple output heads | No activation |
hidden_activations | String or list of string to indicate the activations you want used on the output of hidden layers (not including the output layer), default is ReLU and for example "tanh" would have tanh applied on all hidden layer activations | ReLU after every hidden layer |
dropout | Float to indicate what dropout probability you want applied after each hidden layer | 0 |
initialiser | String to indicate which initialiser you want used to initialise all the parameters | PyTorch & TF Default |
batch_norm | Boolean to indicate whether you want batch norm applied to the output of every hidden layer | False |
columns of_data_to_be_embedded | List to indicate the column numbers of the data that you want to be put through an embedding layer before being fed through the hidden layers of the network | No embeddings |
embedding_dimensions | If you have categorical variables you want embedded before flowing through the network then you specify the embedding dimensions here with a list of the form: [ [embedding_input_dim_1, embedding_output_dim_1], [embedding_input_dim_2, embedding_output_dim_2] ...] | No embeddings |
y_range | Tuple of float or integers of the form (y_lower, y_upper) indicating the range you want to restrict the output values to in regression tasks | No range |
random_seed | Integer to indicate the random seed you want to use | 0 |
return_final_seq_only | Only needed for RNN. Boolean to indicate whether you only want to return the output for the final timestep (True) or if you want to return the output for all timesteps (False) | True |
每种网络类型对input-dim和layers-info的要求略有不同,如下所述:
1.nn
- 输入维度:pytorch中的功能,tensorflow不需要
- layers\u info:表示每个线性层所需隐藏单元数的整数列表。
- 例如:
from nn_builder.pytorch.NN import NN
model = NN(input_dim=5, layers_info=[10, 10, 1], output_activation=None, hidden_activations="relu",
dropout=0.0, initialiser="xavier", batch_norm=False)
2。CNN
- input_dim:pytorch中的(channels,height,width),tensorflow不需要
- layers\u info:我们希望字段layers\u info是一个列表列表,指示所需层的大小和类型。CNN中的每一层都可以是以下四种形式之一:
- [“conv”,频道,内核大小,跨距,填充]
- [“maxpool”,内核大小,跨距,填充]
- [“avgpool”,内核大小,跨距,填充]
- [“线性”,单位]
- 对于pytorch网络内核大小,跨距、填充和单位必须是整数。对于tensorflow,它们都必须是整数,除了padding必须是{“valid”,“same”}中的一个
- 例如:
from nn_builder.pytorch.CNN import CNN
model = CNN(input_dim=(3, 64, 64),
layers_info=[["conv", 32, 3, 1, 0], ["maxpool", 2, 2, 0],
["conv", 64, 3, 1, 2], ["avgpool", 2, 2, 0],
["linear", 10]],
hidden_activations="relu", output_activation="softmax", dropout=0.0,
initialiser="xavier", batch_norm=True)
3。RNN
- 输入维度:pytorch中的功能,tensorflow不需要
- layers\u info:我们希望字段layers\u info是一个列表列表,指示所需层的大小和类型。CNN中的每一层都可以是以下四种形式之一:
- [“LSTM”,单位]
- [“GRU”,单位]
- [“线性”,单位]
- 例如:
from nn_builder.pytorch.CNN import CNN
model = RNN(input_dim=5, layers_info=[["gru", 50], ["lstm", 10], ["linear", 2]],
hidden_activations="relu", output_activation="softmax",
batch_norm=False, dropout=0.0, initialiser="xavier")
贡献
我们可以一起创造出对成千上万人有用的东西。任何人都非常欢迎通过拉式请求贡献。请看issues 页面上有关于最佳贡献领域的想法,并尝试:
- 将测试添加到包含您编写的任何代码的“测试”文件夹中
- 为每个函数编写注释
- 创建一个colab笔记本,演示您创建的任何额外功能是如何工作的