Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow变量值在同一训练集中不同_Tensorflow_Machine Learning_Neural Network_Deep Learning_Data Science - Fatal编程技术网

Tensorflow变量值在同一训练集中不同

Tensorflow变量值在同一训练集中不同,tensorflow,machine-learning,neural-network,deep-learning,data-science,Tensorflow,Machine Learning,Neural Network,Deep Learning,Data Science,我在Python3.6上建立了一个神经网络模型 我试图根据lat、lng、到公共交通的距离、建成年份等属性来预测公寓的价格 我对模型使用相同的训练集。然而,每次我打印出隐藏层中变量的值都是不同的 testing_df_w_price = testing_df.copy() testing_df.drop('PricePerSq',axis = 1, inplace = True) training_df, testing_df = training_df.drop(['P

我在Python3.6上建立了一个神经网络模型

我试图根据lat、lng、到公共交通的距离、建成年份等属性来预测公寓的价格

我对模型使用相同的训练集。然而,每次我打印出隐藏层中变量的值都是不同的

    testing_df_w_price = testing_df.copy()
    testing_df.drop('PricePerSq',axis = 1, inplace = True)
    training_df, testing_df = training_df.drop(['POID'], axis=1), testing_df.drop(['POID'], axis=1)

    col_train = list(training_df.columns)
    col_train_bis = list(training_df.columns)

    col_train_bis.remove('PricePerSq')
    mat_train = np.matrix(training_df)
    mat_test  = np.matrix(testing_df)
    mat_new = np.matrix(training_df.drop('PricePerSq', axis = 1))
    mat_y = np.array(training_df.PricePerSq).reshape((training_df.shape[0],1))

    prepro_y = MinMaxScaler()
    prepro_y.fit(mat_y)

    prepro = MinMaxScaler()
    prepro.fit(mat_train)

    prepro_test = MinMaxScaler()
    prepro_test.fit(mat_new)

    train = pd.DataFrame(prepro.transform(mat_train),columns = col_train)
    test  = pd.DataFrame(prepro_test.transform(mat_test),columns = col_train_bis)

    # List of features
    COLUMNS = col_train
    FEATURES = col_train_bis
    LABEL = "PricePerSq"

    # Columns for tensorflow
    feature_cols = [tf.contrib.layers.real_valued_column(k) for k in FEATURES]

    # Training set and Prediction set with the features to predict
    training_set = train[COLUMNS]
    prediction_set = train.PricePerSq

    # Train and Test
    x_train, x_test, y_train, y_test = train_test_split(training_set[FEATURES] , prediction_set, test_size=0.25, random_state=42)

    y_train = pd.DataFrame(y_train, columns = [LABEL])

    training_set = pd.DataFrame(x_train, columns = FEATURES).merge(y_train, left_index = True, right_index = True) # good

    # Training for submission
    training_sub = training_set[col_train] # good

    # Same thing but for the test set
    y_test = pd.DataFrame(y_test, columns = [LABEL])
    testing_set = pd.DataFrame(x_test, columns = FEATURES).merge(y_test, left_index = True, right_index = True) # good

    # Model
    # tf.logging.set_verbosity(tf.logging.INFO)
    tf.logging.set_verbosity(tf.logging.ERROR)
    regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
                                              hidden_units=[int(len(col_train)+1/2)],
                                              model_dir = "/tmp/tf_model")
    for k in regressor.get_variable_names():
        print(k)
        print(regressor.get_variable_value(k))

当您构建网络时,变量用随机值初始化。由于损失函数可能存在许多局部极小值,因此每次运行网络时,拟合参数都会发生变化。
另外,如果你的损失函数是凸的(只有一个(全局)极小值),那么变量的顺序是任意的。例如,如果将一个具有1个隐藏层和2个隐藏节点的网络拟合,则第一次运行中节点1的参数可能对应于节点2的参数,反之亦然。

在机器学习中,神经网络的当前“知识状态”通过图中连接的权重表示。一般认为,您的整个网络代表一个高维函数,学习任务意味着找到此函数的全局最优值。学习过程根据指定的优化器更改神经网络中连接的权重,在您的情况下,这是默认的
tf.contrib.learn.DNNRegressor
(Adagrad优化器)。但还有其他参数会影响模型中的最终“知识状态”。例如,有(我保证以下列表不完整):

  • 模型中的初始学习速率
  • 随时间调整学习速率的学习速率计划
  • 最终确定了规律和提前停止
  • 用于权重初始化的初始化策略(例如He初始化或随机初始化)

Plus(这可能是最重要的事情,了解为什么每次训练后你的体重都不同),你必须考虑在训练过程中使用<强>随机<强>梯度下降算法。这意味着,对于每个优化步骤,算法选择整个训练集的随机子集。因此,一个优化步骤并不总是指向高维函数的全局最优值,而是指向可以用随机选择的子集计算的最陡下降。由于优化过程中的这种随机成分,您可能永远无法达到任务的全局最优。但是通过仔细选择超参数(当然还有好的数据),你会得到一个很好的近似解,它位于函数的局部最优,并且每次你重新训练模型时都会改变

总之,不要通过权重来判断模型的性能,因为每次权重都会略有不同。使用性能度量,如交叉验证中计算的精度或测试集上计算的混淆矩阵


p.S.
tf.contrib.learn.DNNRegressor
是最新TensorFlow发行版中不推荐使用的函数,如中所示。请改用
tf.estimator.DNNRegressor

请不要发布代码图像,而是发布代码本身。谢谢,我已经编辑了这篇文章。