Skip to content

Commit

Permalink
Merge pull request #419 from adamryczkowski/master
Browse files Browse the repository at this point in the history
Julia 1.0 compatability
  • Loading branch information
malmaud authored Aug 29, 2018
2 parents 3c3977a + 111178c commit ce253c7
Show file tree
Hide file tree
Showing 59 changed files with 544 additions and 479 deletions.
21 changes: 8 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@ os:
- osx
- linux
julia:
- 0.6
- nightly
- 0.7
- 1.0
env:
- CONDA_JL_VERSION="2" PYTHON=""
- CONDA_JL_VERSION="3" PYTHON=""
notifications:
email: false
script:
- julia -e 'Pkg.clone(pwd())'
- julia -e 'Pkg.build("TensorFlow")'
- julia -e 'Pkg.test("TensorFlow", coverage=true)'
- julia -e 'using Pkg; Pkg.clone(pwd())'
- julia -e 'using Pkg; Pkg.build("TensorFlow")'
- julia -e 'using Pkg; Pkg.test("TensorFlow", coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("TensorFlow")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
- julia -e 'Pkg.add("Documenter")'
- julia -e 'cd(Pkg.dir("TensorFlow")); include(joinpath("docs", "make.jl"))'
matrix:
fast_finish: true
allow_failures:
- julia: nightly
- os: osx
- julia -e 'using Pkg; cd(Pkg.dir("TensorFlow")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
- julia -e 'using Pkg; Pkg.add("Documenter")'
- julia -e 'using Pkg; cd(Pkg.dir("TensorFlow")); include(joinpath("docs", "make.jl"))'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ saver = train.Saver()
# Run training
run(sess, global_variables_initializer())
checkpoint_path = mktempdir()
info("Checkpoint files saved in $checkpoint_path")
@info("Checkpoint files saved in $checkpoint_path")
for epoch in 1:100
cur_loss, _ = run(sess, [Loss, minimize_op], Dict(X=>x, Y_obs=>y))
println(@sprintf("Current loss is %.2f.", cur_loss))
Expand Down
10 changes: 7 additions & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
julia 0.6
ProtoBuf 0.3.0
julia 0.7
ProtoBuf 0.6.1
PyCall 1.7.1
TakingBroadcastSeriously 0.1.1
Conda 0.6.0
Distributions 0.10.2
StatsFuns 0.3.0
SpecialFunctions v0.7.0
JLD2 0.0.6
FileIO 0.1.2
Juno 0.2.3
Compat 0.18
MacroTools 0.3.6
AutoHashEquals 0.1.0
MNIST 0.0.2
Nullables 0.0.7
SpecialFunctions 0.7.0
1 change: 1 addition & 0 deletions deps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build.log
22 changes: 11 additions & 11 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cur_py_version = "1.8.0"
# Error message for Windows
############################

if is_windows()
if Sys.iswindows()
error("TensorFlow.jl does not support Windows. Please see https://github.com/malmaud/TensorFlow.jl/issues/204")
end

Expand All @@ -19,15 +19,15 @@ end

use_gpu = "TF_USE_GPU" keys(ENV) && ENV["TF_USE_GPU"] == "1"

if is_apple() && use_gpu
warn("No support for TF_USE_GPU on OS X - to enable the GPU, build TensorFlow from source. Falling back to CPU")
if Sys.isapple() && use_gpu
@warn("No support for TF_USE_GPU on OS X - to enable the GPU, build TensorFlow from source. Falling back to CPU")
use_gpu=false
end

if use_gpu
info("Building TensorFlow.jl for use on the GPU")
@info("Building TensorFlow.jl for use on the GPU")
else
info("Building TensorFlow.jl for CPU use only. To enable the GPU, set the TF_USE_GPU environment variable to 1 and rebuild TensorFlow.jl")
@info("Building TensorFlow.jl for CPU use only. To enable the GPU, set the TF_USE_GPU environment variable to 1 and rebuild TensorFlow.jl")
end


Expand Down Expand Up @@ -79,24 +79,24 @@ function download_and_unpack(url)
run(`tar -xzf $tensorflow_zip_path -C downloads`)
end

@static if is_apple()
@static if Sys.isapple()
if use_gpu
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-darwin-x86_64-$cur_version.tar.gz"
else
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-$cur_version.tar.gz"
end
download_and_unpack(url)
mv("$lib_dir/libtensorflow.so", "usr/bin/libtensorflow.dylib", remove_destination=true)
mv("$lib_dir/libtensorflow_framework.so", "usr/bin/libtensorflow_framework.so", remove_destination=true)
mv("$lib_dir/libtensorflow.so", "usr/bin/libtensorflow.dylib", force=true)
mv("$lib_dir/libtensorflow_framework.so", "usr/bin/libtensorflow_framework.so", force=true)
end

@static if is_linux()
@static if Sys.islinux()
if use_gpu
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-$cur_version.tar.gz"
else
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-$cur_version.tar.gz"
end
download_and_unpack(url)
mv("$lib_dir/libtensorflow.so", "usr/bin/libtensorflow.so", remove_destination=true)
mv("$lib_dir/libtensorflow_framework.so", "usr/bin/libtensorflow_framework.so", remove_destination=true)
mv("$lib_dir/libtensorflow.so", "usr/bin/libtensorflow.so", force=true)
mv("$lib_dir/libtensorflow_framework.so", "usr/bin/libtensorflow_framework.so", force=true)
end
2 changes: 1 addition & 1 deletion docs/src/logistic.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ saver = train.Saver()
# Run training
run(sess, global_variables_initializer())
checkpoint_path = mktempdir()
info("Checkpoint files saved in $checkpoint_path")
@info("Checkpoint files saved in $checkpoint_path")
for epoch in 1:100
cur_loss, _ = run(sess, (Loss, minimize_op), Dict(X=>x, Y_obs=>y))
println(@sprintf("Current loss is %.2f.", cur_loss))
Expand Down
8 changes: 4 additions & 4 deletions docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ end
### Evaluate the model

```julia
correct_prediction = indmax(y, 2) .== indmax(y_, 2)
correct_prediction = argmax(y, 2) .== argmax(y_, 2)
accuracy=reduce_mean(cast(correct_prediction, Float32))
testx, testy = load_test_set()

Expand Down Expand Up @@ -137,7 +137,7 @@ cross_entropy = reduce_mean(-reduce_sum(y_.*log(y_conv), axis=[2]))

train_step = train.minimize(train.AdamOptimizer(1e-4), cross_entropy)

correct_prediction = indmax(y_conv, 2) .== indmax(y_, 2)
correct_prediction = argmax(y_conv, 2) .== argmax(y_, 2)

accuracy = reduce_mean(cast(correct_prediction, Float32))

Expand All @@ -147,12 +147,12 @@ for i in 1:1000
batch = next_batch(loader, 50)
if i%100 == 1
train_accuracy = run(session, accuracy, Dict(x=>batch[1], y_=>batch[2], keep_prob=>1.0))
info("step $i, training accuracy $train_accuracy")
@info("step $i, training accuracy $train_accuracy")
end
run(session, train_step, Dict(x=>batch[1], y_=>batch[2], keep_prob=>.5))
end

testx, testy = load_test_set()
test_accuracy = run(session, accuracy, Dict(x=>testx, y_=>testy, keep_prob=>1.0))
info("test accuracy $test_accuracy")
@info("test accuracy $test_accuracy")
```
8 changes: 4 additions & 4 deletions examples/ae.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ end
num_features = 28^2


include(Pkg.dir("TensorFlow","examples","mnist_loader.jl"))
include(Pkg.dir("TensorFlow","src","layers","fully_connected.jl"))
include(joinpath(dirname(pathof(TensorFlow)), "..", "examples","mnist_loader.jl"))
include(joinpath(dirname(pathof(TensorFlow)), "..", "src","layers","fully_connected.jl"))
loader = DataLoader()
session = Session(Graph())

Expand Down Expand Up @@ -77,15 +77,15 @@ history = MVHistory()
push!(history,:loss_val, epoch, val_loss)
plot(history, reuse=true)
scatter3d(center[:,1],center[:,2],center[:,3], zcolor = testy, legend=false, title="Latent space", reuse=true)
info("step $epoch, training loss $train_loss, time taken: $(printtime(t0))")
@info("step $epoch, training loss $train_loss, time taken: $(printtime(t0))")
train.save(saver, session, joinpath(checkpoint_path, "ae_mnist"), global_step=epoch)
end
run(session, train_step, Dict(x=>batch))
end
end

test_loss, center, reconstruction = run(session, [loss_MSE, l_z, l_out], Dict(x=>testx))
info("test accuracy $test_loss")
@info("test accuracy $test_loss")

# Plot som example reconstructions
offset = 0
Expand Down
5 changes: 3 additions & 2 deletions examples/logistic.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using TensorFlow
using Distributions
using Printf

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

function draw(probs)
y = zeros(size(probs))
Expand Down Expand Up @@ -39,7 +40,7 @@ saver = train.Saver()
# Run training
run(sess, global_variables_initializer())
checkpoint_path = mktempdir()
info("Checkpoint files saved in $checkpoint_path")
@info("Checkpoint files saved in $checkpoint_path")
for epoch in 1:100
cur_loss, _ = run(sess, [Loss, minimize_op], Dict(X=>x, Y_obs=>y))
println(@sprintf("Current loss is %.2f.", cur_loss))
Expand Down
6 changes: 3 additions & 3 deletions examples/mnist_full.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ end

train_step = train.minimize(train.AdamOptimizer(1e-4), cross_entropy)

correct_prediction = indmax(y_conv, 2) .== indmax(y_, 2)
correct_prediction = argmax(y_conv, 2) .== argmax(y_, 2)

accuracy = reduce_mean(cast(correct_prediction, Float32))

Expand All @@ -72,13 +72,13 @@ for i in 1:200
batch = next_batch(loader, 50)
if i%100 == 1
train_accuracy = run(session, accuracy, Dict(x=>batch[1], y_=>batch[2], keep_prob=>1.0))
info("step $i, training accuracy $train_accuracy")
@info("step $i, training accuracy $train_accuracy")
end
run(session, train_step, Dict(x=>batch[1], y_=>batch[2], keep_prob=>.5))
end

testx, testy = load_test_set()
test_accuracy = run(session, accuracy, Dict(x=>testx, y_=>testy, keep_prob=>1.0))
info("test accuracy $test_accuracy")
@info("test accuracy $test_accuracy")

visualize()
3 changes: 2 additions & 1 deletion examples/mnist_loader.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using MNIST
import Random

mutable struct DataLoader
cur_id::Int
order::Vector{Int}
end

DataLoader() = DataLoader(1, shuffle(1:60000))
DataLoader() = DataLoader(1, Random.shuffle(1:60000))

function next_batch(loader::DataLoader, batch_size)
x = zeros(Float32, batch_size, 784)
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ y = nn.softmax(x*W + b)
cross_entropy = reduce_mean(-reduce_sum(y_ .* log(y), axis=[2]))
train_step = train.minimize(train.GradientDescentOptimizer(.00001), cross_entropy)

correct_prediction = indmax(y, 2) .== indmax(y_, 2)
correct_prediction = argmax(y, 2) .== argmax(y_, 2)
accuracy=reduce_mean(cast(correct_prediction, Float32))

for i in 1:1000
Expand Down
19 changes: 13 additions & 6 deletions src/TensorFlow.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
__precompile__(true)
module TensorFlow

warn("Loading a new version of TensorFlow.jl for the first time. This initial load can take around 5 minutes as code is precompiled; subsequent usage will only take a few seconds.")
@warn("Loading a new version of TensorFlow.jl for the first time. This initial load can take around 5 minutes as code is precompiled; subsequent usage will only take a few seconds.")

export
Graph,
Expand Down Expand Up @@ -127,10 +126,17 @@ import_op,
@tfimport,
tf_versioninfo


using Distributed

const pyproc = Ref(0)

function deallocator(data, len, arg)

end

function __init__()
c_deallocator[] = cfunction(deallocator, Void, (Ptr{Void}, Csize_t, Ptr{Void}))
c_deallocator[] = @cfunction(deallocator, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid}))
end

function load_python_process(;force_reload=false)
Expand All @@ -139,8 +145,9 @@ function load_python_process(;force_reload=false)
addprocs(1)
pyproc[] = nprocs()
py_file = joinpath(dirname(@__FILE__), "py.jl")
eval(Main, quote
Base.eval(Main, quote
# These have to be split for unclear reasons on .6
using Distributed
remotecall_wait($(pyproc[]), $py_file) do py_file
include(py_file)
end
Expand All @@ -151,7 +158,7 @@ function load_python_process(;force_reload=false)
py_version_check()
return pyproc[]
else
remotecall_fetch(1) do
Distributed.remotecall_fetch(1) do
load_python_process()
end
end
Expand All @@ -169,7 +176,7 @@ Returns a future to the result.
"""
macro py_proc(expr)
quote
eval(Main, quote
Base.eval(Main, quote
remotecall_wait($(TensorFlow.load_python_process())) do
$($(Expr(:quote, expr)))
end
Expand Down
Loading

0 comments on commit ce253c7

Please sign in to comment.