Git Product home page Git Product logo

tensorflow.jl's Introduction

TensorFlow

Build Status codecov.io

A wrapper around TensorFlow, a popular open source machine learning framework from Google.

Documentation

Documentation available here.

Why use TensorFlow.jl?

See a list of advantages over the Python API.

What's changed recently?

See NEWS.

Basic usage

using TensorFlow

sess = TensorFlow.Session()

x = TensorFlow.constant(Float64[1,2])
y = TensorFlow.Variable(Float64[3,4])
z = TensorFlow.placeholder(Float64)

w = exp(x + z + -y)

run(sess, TensorFlow.global_variables_initializer())
res = run(sess, w, Dict(z=>Float64[1,2]))
Base.Test.@test res[1] โ‰ˆ exp(-1)

Installation

Install via

Pkg.add("TensorFlow")

To enable support for GPU usage (Linux only), set an environment variable TF_USE_GPU to "1" and then rebuild the package. eg

ENV["TF_USE_GPU"] = "1"
Pkg.build("TensorFlow")

CUDA 8.0 and cudnn are required for GPU usage. If you need to use a different version of CUDA you can compile libtensorflow from source.

Installation via Docker

Simply run docker run -it malmaud/julia:tf to open a Julia REPL that already has TensorFlow installed:

julia> using TensorFlow
julia>

For a version of TensorFlow.jl that utilizes GPUs, use nvidia-docker run -it malmaud/julia:tf_gpu. Download nvidia-docker if you don't already have it.

Logistic regression example

Realistic demonstration of using variable scopes and advanced optimizers

using Distributions

# Generate some synthetic data
x = randn(100, 50)
w = randn(50, 10)
y_prob = exp(x*w)
y_prob ./= sum(y_prob,2)

function draw(probs)
    y = zeros(size(probs))
    for i in 1:size(probs, 1)
        idx = rand(Categorical(probs[i, :]))
        y[i, idx] = 1
    end
    return y
end

y = draw(y_prob)

# Build the model
sess = Session(Graph())
X = placeholder(Float64)
Y_obs = placeholder(Float64)

variable_scope("logistic_model", initializer=Normal(0, .001)) do
    global W = get_variable("weights", [50, 10], Float64)
    global B = get_variable("bias", [10], Float64)
end

Y=nn.softmax(X*W + B)
Loss = -reduce_sum(log(Y).*Y_obs)
optimizer = train.AdamOptimizer()
minimize_op = train.minimize(optimizer, Loss)
saver = train.Saver()
# Run training
run(sess, global_variables_initializer())
checkpoint_path = mktempdir()
info("Checkpoint files saved in $checkpoint_path")
for epoch in 1:100
    cur_loss, _ = run(sess, vcat(Loss, minimize_op), Dict(X=>x, Y_obs=>y))
    println(@sprintf("Current loss is %.2f.", cur_loss))
    train.save(saver, sess, joinpath(checkpoint_path, "logistic"), global_step=epoch)
end

Troubleshooting

If you see issues from the ccall or python interop, try updating TensorFlow both in Julia and in the global python install:

julia> Pkg.build("TensorFlow")
$ pip install --upgrade tensorflow

Optional: Using a custom TensorFlow binary

If you already have a version of libtensorflow.so or libtensorflow.dylib that you would like to use, you can set the environment variable LIBTENSORFLOW to its path. By default, TensorFlow.jl will use the library in TensorFlow.jl/deps/usr/bin.

If you want to build your own version of the TensorFlow binary library instead of relying on the one that is installed with Pkg.build("TensorFlow"), follow the instructions from https://www.tensorflow.org/install/install_sources, except:

  • In the section "Build the pip package", instead run bazel build --config=opt //tensorflow:libtensorflow.so.
  • Then copy the file "bazel-bin/tensorflow/libtensorflow.so" to the "deps/usr/bin" directory in the TensorFlow.jl package.
  • On OS X, rename the file to libtensorflow.dylib.

A convenience script is included to use Docker to easily build the library. Just install docker and run julia build_libtensorflow.so from the "deps" directory of the TensorFlow.jl package.

tensorflow.jl's People

Contributors

malmaud avatar kshyatt avatar oxinabox avatar mikeinnes avatar yeesian avatar baggepinnen avatar pevnak avatar asimshankar avatar tkelman avatar lmescheder avatar staticfloat avatar hpoit avatar wookay avatar karenl7 avatar domluna avatar schmrlng avatar jiahao avatar william-silversmith avatar

Watchers

Christoph Lassner avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.