Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conv1d #506

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ProtoBuf = "3349acd9-ac6a-5e09-bcdb-63829b23a429"
Expand Down
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ MacroTools 0.3.6
AutoHashEquals 0.1.0
MLDatasets 0.3.0
SpecialFunctions 0.7.0
Optim 0.17.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now delete the REQUIRE file

3 changes: 3 additions & 0 deletions deps/default_imports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,6 @@ Rank
Conv2DBackpropInput
Svd
Cross
FFT
ComplexAbs
MatrixSolve
1 change: 1 addition & 0 deletions src/TensorFlow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ with_tape


using Distributed
using Optim
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in This PR?


# Load these packages here so they are available to the additional
# process spawned in 'load_python_process. Arslan thinks that will
Expand Down
19 changes: 19 additions & 0 deletions src/ops/nn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ import .rnn_cell: zero_state, output_size, state_size
conv2d(input, filter; padding=padding, strides=strides, kwargs...)
end

@tf.op function conv1d(input, filter_, strides_::Int64, padding::String; data_format="NHWC", kwargs...)
spatial_start_dim = 0
if data_format=="NHWC"
strides_ = [1,1,strides_,1]
spatial_start_dim = 2
elseif data_format == "NCHW" || data_format == "NCW"
data_format = "NCHW"
spatial_start_dim = 3
strides_ = [1,1,1,strides_]
else
@error "data_format must be NHWC or NCHW or NCW"
end
input = Ops.expand_dims(input, spatial_start_dim)
filter_ = Ops.expand_dims(filter_, 1)
result = Ops.conv2d(input, filter_; strides = strides_, padding = padding, data_format=data_format, kwargs...)
result = Ops.squeeze(result, squeeze_dims=[spatial_start_dim-1])
return result
end

# Same for max pool
@tf.op function max_pool(input, ksize, strides, padding; kwargs...)
max_pool(input; ksize=ksize, strides=strides, padding=padding, kwargs...)
Expand Down
20 changes: 20 additions & 0 deletions test/nn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ using StatsFuns
using Random
import LinearAlgebra

@testset "conv1d" begin
let
sess = Session(Graph())
F = zeros(Float32, 2, 3, 4) # batch_size = 2, dimension = 3, channle = 4
for i = 1:2
for j = 1:3
for k = 1:4
F[i,j,k] = Float32(i+j+k-3)
end
end
end
input = constant(F)
filter_ = constant(ones(Float32, 3, 4, 1)) # width = 3, input channel = 4 output channel = 1
output = nn.conv1d(input, filter_, 2, "VALID")
output_val = run(sess, output)
ref_val = reshape(Float32[30.0;42.0], 2, 1, 1)
@test ref_val ≈ output_val
end
end

@testset "conv2d_transpose" begin
let
sess = Session(Graph())
Expand Down