TensorFlow LlinearRegressor示例无法在路径C:\ Users中创建目录并引发异常

时间:2017-04-10 06:25:51

标签: python tensorflow

我正在学习机器学习。与此同时,我使用TensorFlow。

的Python :3.5.2

系统:Windows 10

TensorFlow :1.0.1,由pip安装

<小时/> 但是当我运行以下代码时遇到一些问题。代码为TensorFlow Basic usage

import tensorflow as tf
import numpy as np

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4, 
  num_epochs=1000)
estimator.fit(input_fn=input_fn, steps=1000)
print(estimator.evaluate(input_fn=input_fn))

<小时/> 结果如下:

    WARNING:tensorflow:Using temporary folder as model directory: C:\Users\赵子龙\AppData\Local\Temp\tmp_dq8vxf6
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "CountExtremelyRandomStats" device_type: "CPU"') for unknown op: CountExtremelyRandomStats
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "FinishedNodes" device_type: "CPU"') for unknown op: FinishedNodes
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "GrowTree" device_type: "CPU"') for unknown op: GrowTree
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ReinterpretStringToFloat" device_type: "CPU"') for unknown op: ReinterpretStringToFloat
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "SampleInputs" device_type: "CPU"') for unknown op: SampleInputs
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ScatterAddNdim" device_type: "CPU"') for unknown op: ScatterAddNdim
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNInsert" device_type: "CPU"') for unknown op: TopNInsert
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNRemove" device_type: "CPU"') for unknown op: TopNRemove
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TreePredictions" device_type: "CPU"') for unknown op: TreePredictions
    E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "UpdateFertileSlots" device_type: "CPU"') for unknown op: UpdateFertileSlots
    WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
    WARNING:tensorflow:From C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
    Instructions for updating:
    Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
    Traceback (most recent call last):
      File "C:\Users\赵子龙\Desktop\Python\ML\ML\LinerRegressor.py", line 10, in <module>
        estimator.fit(input_fn=input_fn, steps=1000)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\util\deprecation.py", line 280, in new_func
        return func(*args, **kwargs)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 426, in fit
        loss = self._train_model(input_fn=input_fn, hooks=hooks)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 981, in _train_model
        config=self.config.tf_config) as mon_sess:
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\monitored_session.py", line 315, in MonitoredTrainingSession
        return MonitoredSession(session_creator=session_creator, hooks=all_hooks)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\monitored_session.py", line 601, in __init__
        session_creator, hooks, should_recover=True)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\monitored_session.py", line 428, in __init__
        h.begin()
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 324, in begin
        self._summary_writer = SummaryWriterCache.get(self._checkpoint_dir)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\summary\writer\writer_cache.py", line 58, in get
        logdir, graph=ops.get_default_graph())
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\summary\writer\writer.py", line 289, in __init__
        event_writer = EventFileWriter(logdir, max_queue, flush_secs)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\summary\writer\event_file_writer.py", line 63, in __init__
        gfile.MakeDirs(self._logdir)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 301, in recursive_create_dir
        pywrap_tensorflow.RecursivelyCreateDir(compat.as_bytes(dirname), status)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
        next(self.gen)
      File "C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 467, in raise_exception_on_not_ok_status
        pywrap_tensorflow.TF_GetCode(status))
    tensorflow.python.framework.errors_impl.InvalidArgumentError: Failed to create a directory: C:\Users/赵子龙

<小时/> 现在我可以删除第一个警告

WARNING:tensorflow:Using temporary folder as model directory: C:\Users\赵子龙\AppData\Local\Temp\tmp_dq8vxf6

通过修改代码
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)

estimator = tf.contrib.learn.LinearRegressor(feature_columns=features,model_dir='/tmp')

<小时/> 更改后,我的代码可以运行良好,没有异常,也没有第一个警告 结果如下:

E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "CountExtremelyRandomStats" device_type: "CPU"') for unknown op: CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "FinishedNodes" device_type: "CPU"') for unknown op: FinishedNodes
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "GrowTree" device_type: "CPU"') for unknown op: GrowTree
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ReinterpretStringToFloat" device_type: "CPU"') for unknown op: ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "SampleInputs" device_type: "CPU"') for unknown op: SampleInputs
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ScatterAddNdim" device_type: "CPU"') for unknown op: ScatterAddNdim
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNInsert" device_type: "CPU"') for unknown op: TopNInsert
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNRemove" device_type: "CPU"') for unknown op: TopNRemove
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TreePredictions" device_type: "CPU"') for unknown op: TreePredictions
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "UpdateFertileSlots" device_type: "CPU"') for unknown op: UpdateFertileSlots
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:From C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:From C:\Users\赵子龙\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
{'global_step': 1000, 'loss': 5.6782592e-09}

但我收到了另一个错误:结果 {&#39; global_step&#39;:1000,&#39;损失&#39;:5.6782592e-09} 与官方回答 {&#39; global_step&#39;:1000,&#39;损失&#39;:1.9650059e-11}

另一个问题:


当我将model_dir='/tmp'更改为model_dir='/Users/tmp'时,我会得到同样的结果 错误,因为我没有添加参数。

P.S:用户是C盘中的目录。 我想知道Users是否是一个不同的目录,因此python无法在Win10中的用户中创建目录。


Quession Summary

  1. 如果我不添加model_dir参数,为什么程序会引发异常?
  2. 为什么我得错了结果?
  3. 为什么程序无法在C:\Users
  4. 中创建目录

    我希望有人可以帮助我:)。

1 个答案:

答案 0 :(得分:0)

  
      
  1. 如果我不添加model_dir参数,为什么程序会引发异常?
  2.   

这不是一个例外,它是一个警告。通常我会在工作目录中创建一个子文件夹来保存输出(For TensorBoard)。

  
      
  1. 为什么我得错了结果?
  2.   

与官方(&#39;亏损&#39;:1.9650059e-11)的结果相比,您的答案(&#39;损失&#39;:5.6782592e-09)相对接近。

不同的结果并不意味着错误的结果。在机器学习中我们尽量减少成本函数,而5.6782592e-09是一个相对较好的结果。此外,如果您尝试多次运行相同的代码,您可以看到它显示不同的结果。

  
      
  1. 为什么程序无法在C:\ Users
  2. 中创建目录   

你可能有一个 PermissionDeniedError ,就像在1中解释的那样。我建议改为使用相对路径:model_dir='./output'