0

Background: I am learning how to work with TesorFlow and I tried my first example eager.ipynb from the get started section. Obviously the notebook is working, but I encountered an Error after I downloaded the eager.py file and ran it locally:

    !head - n5 {train_dataset_fp}
    ^
SyntaxError: invalid syntax

I am not familiar with !head instruction, what is it supposed to do and hot to solve the error? Here are the recommendations I get from PyCharm:

Import from ...

pip._vendor.requests.head()
tensorflow.python.estimator.canned.head
tensorflow.contrib.estimator.python.estimator.head
tensorflow.contrib.timeseries.python.timeseries.head
tensorflow.contrib.gen.python.estimator.python.head
tensorflow.contrib.learn.python.learn.estimators.head

TensorFlow version: 1.7.0

Cezar Cobuz
  • 627
  • 6
  • 23

1 Answers1

1

In IPython, an initial ! means "run system shell command". In this case, if you are in a Posix environment, it head -n5 {train_dataset_fp} shows the first five lines of the file at the path stored in the variable train_dataset_fp. It is just to show you a small sample of the data, it does not do anything significant and you can skip it if you are running your code in a plain Python interpreter. Or, if you really want to run it, you can replace it with a call to the external command from Python:

from subprocess import call
call(['head', '-n5', train_dataset_fp])

Although, still, this works only on Posix(-like) environments, so not on Windows for example.

jdehesa
  • 52,620
  • 6
  • 60
  • 94
  • Should I use Jupyter or some other IDE that supports notebooks in favor of, let's say, PyCharm? – Cezar Cobuz Apr 10 '18 at 10:16
  • 1
    @Elliad That is completely up to you. IPython is great for interactive work, Jupyter too and also for presenting and sharing results, an IDEs like PyCharm is powerful to build more complex programs and deal with code split across several files, and to write standalone scripts in general (e.g. things to run periodically in a server or overnight). Spyder is somewhat in between and JupyterLab makes Jupyter potentially even more useful in general. But you should be able to get exactly the same things to work in any of them. – jdehesa Apr 10 '18 at 10:25