美文网首页
VS Code 配置python 开发tensorflow

VS Code 配置python 开发tensorflow

作者: InnoTech | 来源:发表于2020-04-18 10:15 被阅读0次

    1 关于tensorflow的安装 参看官方文档

    #使用 [Homebrew](https://brew.sh/) 软件包管理器安装:
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
    brew update
    brew install python  # Python 3
    sudo pip3 install -U virtualenv  # system-wide install
    
    
    #创建一个新的虚拟环境,方法是选择 Python 解释器并创建一个 ./venv 目录来存放它:
    virtualenv --system-site-packages -p python3 ./venv
    #使用特定于 shell 的命令激活该虚拟环境:
    source ./venv/bin/activate  # sh, bash, ksh, or zsh
    
    (venv)$  pip install --upgrade pip
    #安装tensorflow
    (venv)$  pip install --upgrade tensorflow
    

    2.安装vs code

    a. 安装插件 Python

    b. (工作目录)/.vscode/settings.json 文件设置如下

    {
        "python.pythonPath": "/Users/Apple/venv/bin/python3",
        "python.autoComplete.extraPaths": [
            "/Users/mirage/venv/lib/python3.7/site-packages/"
        ]
    }
    

    c. (工作目录)/.vscode/launch.json 文件设置如下

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Current File",
                "type": "python",
                "pythonPath": "${config:python.pythonPath}",
                "request": "launch",
                //"program": "${file}",
                "program": "${workspaceRoot}/chapter01.py",
                "console": "integratedTerminal"
            }
        ]
    }
    

    3. 示例代码

    import tensorflow as tf
    import numpy as np
    
    #实例化一个Sequential,并添加一个一层的全连接神经网络
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Dense(input_dim=1,units=1))
    #编译神经网络模型 损失函数用mse,随机梯度下降为optimizer
    model.compile(loss='mse', optimizer='sgd')
    
    #初始化数据 
    #生成10个数据 从1到10
    X = np.linspace(1, 10, 10)
    Y = 2*X
    
    #训练数据 verbose=1 为显示进度信息 epochs=5 训练5期 validation_split表示分离20%的数据用来验证
    model.fit(X, Y, verbose=1, epochs=5, validation_split=0.2)
    
    #保存数据 以及加载数据
    #filename = 'model.h5'
    #model.save(filename)
    #model = tf.keras.models.load_model(filename)
    
    #验证数据
    x = tf.constant([1, 2, 3, 4])
    print(model.predict(x))
    #输出:[[2.0426083]  [4.0222816]  [6.0019546]  [7.981628 ]]
    

    相关文章

      网友评论

          本文标题:VS Code 配置python 开发tensorflow

          本文链接:https://www.haomeiwen.com/subject/cvnmvhtx.html