APP下载

教程|TensorEditor:一个小白都能快速玩转的神经网络搭建工具

消息来源:baojiabao.com 作者: 发布时间:2026-08-03

报价宝综合消息教程|TensorEditor:一个小白都能快速玩转的神经网络搭建工具


机器之心整理


参与:思源




近日,机器之心发现一个非常有意思的工具,可以用可视化的方式轻松添加卷积层、全连接层和池化层等层级,然后生成可执行的 TensorFlow 代码。此外,我们也尝试搭建一个简单的卷捷豹构,并在本地 TensorFlow 环境下测试生成的代码。




工具地址:https://www.tensoreditor.com/



TensorEditor 是一个强大的机器学习工具,甚至小白都能以可视化的方式快速生成整个模型的代码。通过 TensorEditor,小白可以连接卷积层、全连接层和池化层等可视化结点创建整个模型,且我们可以将它们转化为 TensorFlow 和 Python 代码,并进一步在自己的环境中运行。




基本上,TensorEditor 的步骤即定义我们的数据集、图像或特征,然后创建深度神经网络并下载 Python 2.7 的代码,最后就需要在我们自己的 TensorFLow 环境下运行就好了。




通过 TensorEditor,我们不仅可以创建深度网络并避免一些常见的代码问题,同时还能生成基于 TensorFlow Estimator 的高效代码。如下所示,机器之心尝试构建了一个简单的卷积网络,我们使用了两个卷积层、两个池化层和一个全连接层,并在最后的 Estimator 使用了交叉熵损失函数和 Adagrad 最优化方法。






上述简单搭建的卷积网络同样可以生成完全可执行的代码,这样可以避免大量的一般代码问题与重复性工作。



  1. import

    tensorflow as tf

  2. import

    pandas as pd

  3. tf

    .

    logging

    .

    set_verbosity

    (

    tf

    .

    logging

    .

    INFO

    )

  4. project_name

    =

    "CNN"

  5. train_csv_file

    =

    ""

  6. test_csv_file

    =

    ""

  7. image_resize

    =[

    28

    ,

    28

    ]

  8. def model_fn

    (

    features

    ,

    labels

    ,

    mode

    ,

    params

    ):

  9.    convolutional_2d_1

    =

    tf

    .

    layers

    .

    conv2d

    (

  10.            inputs

    =

    features

    ,

  11.            filters

    =

    32

    ,

  12.            kernel_size

    =[

    3

    ,

    3

    ],

  13.            strides

    =(

    1

    ,

    1

    ),

  14.            padding

    =

    "same"

    ,

  15.            data_format

    =

    "channels_last"

    ,

  16.            dilation_rate

    =(

    1

    ,

    1

    ),

  17.            activation

    =

    tf

    .

    nn

    .

    relu

    ,

  18.            use_bias

    =

    True

    )

  19.    max_pool_2d_1

    =

    tf

    .

    layers

    .

    max_pooling2d

    (

  20.        inputs

    =

    convolutional_2d_1

    ,

  21.        pool_size

    =[

    2

    ,

    2

    ],

  22.        strides

    =[

    2

    ,

    2

    ],

  23.        padding

    =

    "same"

    ,

  24.        data_format

    =

    "channels_last"

    )

  25.    convolutional_2d_2

    =

    tf

    .

    layers

    .

    conv2d

    (

  26.            inputs

    =

    max_pool_2d_1

    ,

  27.            filters

    =

    64

    ,

  28.            kernel_size

    =[

    3

    ,

    3

    ],

  29.            strides

    =(

    1

    ,

    1

    ),

  30.            padding

    =

    "same"

    ,

  31.            data_format

    =

    "channels_last"

    ,

  32.            dilation_rate

    =(

    1

    ,

    1

    ),

  33.            activation

    =

    tf

    .

    nn

    .

    relu

    ,

  34.            use_bias

    =

    True

    )

  35.    max_pool_2d_2

    =

    tf

    .

    layers

    .

    max_pooling2d

    (

  36.        inputs

    =

    max_pool_2d_1

    ,

  37.        pool_size

    =[

    2

    ,

    2

    ],

  38.        strides

    =[

    2

    ,

    2

    ],

  39.        padding

    =

    "same"

    ,

  40.        data_format

    =

    "channels_last"

    )

  41.    convolutional_2d_3

    =

    tf

    .

    layers

    .

    conv2d

    (

  42.            inputs

    =

    max_pool_2d_2

    ,

  43.            filters

    =

    128

    ,

  44.            kernel_size

    =[

    3

    ,

    3

    ],

  45.            strides

    =(

    1

    ,

    1

    ),

  46.            padding

    =

    "same"

    ,

  47.            data_format

    =

    "channels_last"

    ,

  48.            dilation_rate

    =(

    1

    ,

    1

    ),

  49.            activation

    =

    tf

    .

    nn

    .

    relu

    ,

  50.            use_bias

    =

    True

    )

  51.    max_pool_2d_3

    =

    tf

    .

    layers

    .

    max_pooling2d

    (

  52.        inputs

    =

    convolutional_2d_3

    ,

  53.        pool_size

    =[

    2

    ,

    2

    ],

  54.        strides

    =[

    2

    ,

    2

    ],

  55.        padding

    =

    "same"

    ,

  56.        data_format

    =

    "channels_last"

    )

  57.    flatten_1

    =

    tf

    .

    reshape

    (

    max_pool_2d_3

    ,

    [-

    1

    ,

    2048

    ])

  58.    dense_1

    =

    tf

    .

    layers

    .

    dense

    (

    inputs

    =

    flatten_1

    ,

    units

    =

    1024

    ,

    activation

    =

    tf

    .

    nn

    .

    relu

    )

  59.    dropout_1

    =

    tf

    .

    layers

    .

    dropout

    (

    inputs

    =

    dense_1

    ,

    rate

    =

    0.4

    ,

    training

    =

    mode

    ==

    tf

    .

    estimator

    .

    ModeKeys

    .

    TRAIN

    )

  60.    dense_2

    =

    tf

    .

    layers

    .

    dense

    (

    inputs

    =

    dropout_1

    ,

    units

    =

    256

    ,

    activation

    =

    tf

    .

    nn

    .

    relu

    )

  61.    logits

    =

    dense_2

  62.    predictions

    =

    {

  63.        

    "classes"

    :

    tf

    .

    argmax

    (

    input

    =

    logits

    ,

    axis

    =

    1

    ),

  64.        

    "probabilities"

    :

    tf

    .

    nn

    .

    softmax

    (

    logits

    ,

    name

    =

    "softmax_tensor"

    )

  65.    

    }

  66.    

    #

    Prediction

    and training

  67.    

    if

    mode

    ==

    tf

    .

    estimator

    .

    ModeKeys

    .

    PREDICT

    :

  68.        

    return

    tf

    .

    estimator

    .

    EstimatorSpec

    (

    mode

    =

    mode

    ,

    predictions

    =

    predictions

    )

  69.    

    #

    Calculate

    Loss

    (

    for

    both TRAIN and EVAL modes

    )

  70.    onehot_labels

    =

    tf

    .

    one_hot

    (

    indices

    =

    tf

    .

    cast

    (

    labels

    ,

    tf

    .

    int32

    ),

    depth

    =

    256

    )

  71.    loss

    =

    tf

    .

    losses

    .

    softmax_cross_entropy

    (

  72.        onehot_labels

    =

    onehot_labels

    ,

    logits

    =

    logits

    )

  73.    

    #

    Compute

    evaluation metrics

    .

  74.    accuracy

    =

    tf

    .

    metrics

    .

    accuracy

    (

    labels

    =

    labels

    ,

  75.                                   predictions

    =

    predictions

    [

    "classes"

    ],

  76.                                   name

    =

    "acc_op"

    )

  77.    metrics

    =

    {

    "accuracy"

    :

    accuracy

    }

  78.    tf

    .

    summary

    .

    scalar

    (

    "accuracy"

    ,

    accuracy

    [

    1

    ])

  79.    

    #

    Configure

    the

    Training

    Op

    (

    for

    TRAIN mode

    )

  80.    

    if

    mode

    ==

    tf

    .

    estimator

    .

    ModeKeys

    .

    TRAIN

    :

  81.        optimizer

    =

    tf

    .

    train

    .

    AdagradOptimizer

    (

    learning_rate

    =

    0.001

    )

  82.        train_op

    =

    optimizer

    .

    minimize

    (

  83.            loss

    =

    loss

    ,

  84.            global_step

    =

    tf

    .

    train

    .

    get_global_step

    ())

  85.        

    return

    tf

    .

    estimator

    .

    EstimatorSpec

    (

    mode

    =

    mode

    ,

    loss

    =

    loss

    ,

    train_op

    =

    train_op

    )

  86.    

    #

    Add

    evaluation metrics

    (

    for

    EVAL mode

    )

  87.    eval_metric_ops

    =

    {

  88.        

    "accuracy"

    :

    tf

    .

    metrics

    .

    accuracy

    (

  89.            labels

    =

    labels

    ,

    predictions

    =

    predictions

    [

    "classes"

    ])}

  90.    

    return

    tf

    .

    estimator

    .

    EstimatorSpec

    (

  91.        mode

    =

    mode

    ,

    loss

    =

    loss

    ,

    eval_metric_ops

    =

    eval_metric_ops

    )

  92. #

    Parse

    CSV input file and resize image

  93. def _parse_csv

    (

    line

    ):

  94.    parsed_line

    =

    tf

    .

    decode_csv

    (

    line

    ,

    [[

    ""

    ],

    []])

  95.    filename

    =

    parsed_line

    [

    0

    ]

  96.    label

    =

    parsed_line

    [

    1

    ]

  97.    image_string

    =

    tf

    .

    read_file

    (

    filename

    )

  98.    image_decoded

    =

    tf

    .

    image

    .

    decode_jpeg

    (

    image_string

    ,

    channels

    =

    3

    )

  99.    image_resized

    =

    tf

    .

    image

    .

    resize_images

    (

    image_decoded

    ,

    image_resize

    )

  100.    image_gray

    =

    tf

    .

    image

    .

    rgb_to_grayscale

    (

    image_resized

    )

  101.    

    return

    image_gray

    ,

    label

  102. def data_train_estimator

    ():

  103.    dataset

    =

    tf

    .

    data

    .

    TextLineDataset

    (

    train_csv_file

    ).

    map

    (

    _parse_csv

    )

     

    #

    Map

    each line to convert the data

  104.    dataset

    =

    dataset

    .

    batch

    (

    100

    )

  105.    dataset

    =

    dataset

    .

    shuffle

    (

    1000

    )

  106.    dataset

    =

    dataset

    .

    repeat

    ()

  107.    iterator

    =

    dataset

    .

    make_one_shot_iterator

    ()

     

    #

    create one shot iterator

  108.    feature

    ,

    label

    =

    iterator

    .

    get_next

    ()

  109.    

    return

    feature

    ,

    label

  110. def data_test_estimator

    ():

  111.    dataset

    =

    tf

    .

    data

    .

    TextLineDataset

    (

    test_csv_file

    ).

    map

    (

    _parse_csv

    )

     

    #

    Map

    each line to convert the data

  112.    dataset

    =

    dataset

    .

    batch

    (

    100

    )

  113.    iterator

    =

    dataset

    .

    make_one_shot_iterator

    ()

     

    #

    create one shot iterator

  114.    feature

    ,

    label

    =

    iterator

    .

    get_next

    ()

  115.    

    return

    feature

    ,

    label

  116. def main

    (

    unused_argv

    ):

  117.    

    #

    MAIN ENTRY

  118.    

    #

    Create

    the

    Estimator

  119.    classifier

    =

    tf

    .

    estimator

    .

    Estimator

    (

  120.        model_fn

    =

    model_fn

    ,

  121.        model_dir

    =

    "/tmp/"

    +

    project_name

    ,

  122.        params

    ={

  123.            

    #

    PARAMS

  124.        

    }

  125.    

    )

  126.    classifier

    .

    train

    (

    input_fn

    =

    data_train_estimator

    ,

    steps

    =

    30000

    )

  127.    eval_results

    =

    classifier

    .

    evaluate

    (

    input_fn

    =

    data_test_estimator

    )

  128.    tf

    .

    summary

    .

    scalar

    (

    "Accuracy"

    ,

    eval_results

    [

    "accuracy"

    ])

  129.    print

    (

    eval_results

    )

  130. if

    __name__

    ==

    "__main__"

    :

  131.    tf

    .

    app

    .

    run

    ()




TensorEditor 主要有以下特点:






  • 易于使用:我们只需要添加模块、连接模块并在最后加入评估模块,就能完成搭建。



  • 由易到难:只需要叠加不同的模块,我们就能创建如 VGG 那样的复杂深度网络。



  • 参数直观:可以轻松修改各结点的配置与参数,从而搭建定制化的深度网络。



  • 生成代码:搭建完深度架构,我们就能直接生成可执行的 TensorFlow 代码(Python 2.7)。




90 秒的 MNIST 教程







最后上面的网络就能生成对应的代码,我们可直接复制到本地代码编辑器中并执行:









本文为机器之心整理,

转载请联系本公众号获得授权



?------------------------------------------------


加入机器之心(全职记者/实习生):hr@jiqizhixin.com


投稿或寻求报道:

content

@jiqizhixin.com


广告&商务合作:bd@jiqizhixin.com




2018-06-03 14:32:00

相关文章