当前位置 博文首页 > 丁劲犇技术发布空间:windows tensorflow2最简配置-无需自行安装

    丁劲犇技术发布空间:windows tensorflow2最简配置-无需自行安装

    作者:[db:作者] 时间:2021-09-19 22:44

    最近帮学校配置深度学习环境,使用Anaconda3 配置 Tensorflow2,比使用Pip方便太多。一直没有试过,特此记录!

    1. 安装Anaconda3:http://mirrors.ustc.edu.cn/help/anaconda.html
    2. 更新
    conda update conda
    conda update anaconda
    conda update python
    conda update --all
    
    1. 创建虚拟环境
      conda create -n tf-gpu
      conda activate -n tf-gpu
    2. 安装
    conda search -c conda tensorflow-gpu
    conda install -c anaconda tensorflow-gpu=2.X
    

    这里需要注意,要加 -c anaconda ,否则,会安装CPU的版本哦。此外,貌似会自动下载 cudatoolkit和cudnn,不会污染系统环境,不需要额外下载。记得上次为了匹配版本,搞了三四天。

    1. 测试
    import tensorflow as tf
    from tensorflow.keras import layers
    import numpy as np;
    from tensorflow.keras import optimizers,losses
    tf.test.is_gpu_available()
    
    model_cnn1 = tf.keras.Sequential();
    model_cnn1.add(layers.Conv1D(6,kernel_size=3,strides=1,activation=tf.nn.tanh)); 
    model_cnn1.add(layers.MaxPooling1D(pool_size=2,strides=2)); 
    model_cnn1.add(layers.Conv1D(16,kernel_size=3,strides=1,activation=tf.nn.tanh)); 
    model_cnn1.add(layers.MaxPooling1D(pool_size=2,strides=2)); 
    model_cnn1.add(layers.Flatten()); 
    
    model_cnn2 = tf.keras.Sequential();
    model_cnn2.add(layers.Conv2D(6,kernel_size=(3,2),strides=1,activation=tf.nn.tanh)); 
    model_cnn2.add(layers.MaxPooling2D(pool_size=(2,3),strides=2)); 
    model_cnn2.add(layers.Conv2D(16,kernel_size=(3,2),strides=1,activation=tf.nn.tanh)); 
    model_cnn2.add(layers.MaxPooling2D(pool_size=(3,2),strides=2)); 
    model_cnn2.add(layers.Flatten());
    
    model_fnn = tf.keras.Sequential();
    model_fnn.add(layers.Dense(64, activation=tf.nn.tanh,kernel_regularizer=tf.keras.regularizers.l1(0.0001)));
    model_fnn.add(layers.Dense(64, activation=tf.nn.tanh,kernel_regularizer=tf.keras.regularizers.l1(0.0001)));
    model_fnn.add(layers.Dense(3, activation=None,kernel_regularizer=tf.keras.regularizers.l1(0.0001)));
    
    input_scalar = tf.keras.Input(shape=(10,))
    input_1d = tf.keras.Input(shape=(1024,3,))
    input_2d = tf.keras.Input(shape=(64,56,2))
    
    data_1d = model_cnn1(input_1d)
    data_2d = model_cnn2(input_2d)
    combined = tf.keras.layers.concatenate([input_scalar,data_1d,data_2d])
    
    final_out = model_fnn(combined)
    
    model_root = tf.keras.Model(inputs = [input_scalar,input_1d,input_2d],outputs=final_out)
    model_root.compile(optimizer=optimizers.Adam(lr=0.001),
        loss=losses.MeanSquaredError(),
        metrics=['accuracy'] 
        );   
    model_root.summary()
    
    sc_1 = tf.random.uniform([100,10]);
    sc_2 = tf.random.uniform([100,1024,3]);
    sc_3 = tf.random.uniform([100,64,56,2]);
    y_data = tf.random.uniform([100,3]);
    history = model_root.fit([sc_1,sc_2,sc_3], y_data,epochs=32);
    
    
    cs