Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pytorch Without Pytorch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Falguni Ghosh
Pytorch Without Pytorch
Commits
7bace268
Commit
7bace268
authored
Oct 15, 2023
by
Falguni Ghosh
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
35ca4f8b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
2_CNN/FullyConnected.py
+71
-0
71 additions, 0 deletions
2_CNN/FullyConnected.py
with
71 additions
and
0 deletions
2_CNN/FullyConnected.py
0 → 100644
+
71
−
0
View file @
7bace268
from
.Base
import
BaseLayer
import
numpy
as
np
import
copy
class
FullyConnected
(
BaseLayer
):
input_size
=
None
output_size
=
None
error_tensor
=
None
input_tensor
=
None
def
__init__
(
self
,
input_size
,
output_size
):
super
().
__init__
()
self
.
trainable
=
True
self
.
weights
=
np
.
random
.
uniform
(
0.0
,
1.0
,
(
input_size
+
1
,
output_size
))
#removed
self
.
input_size
=
input_size
self
.
output_size
=
output_size
self
.
_optimizer
=
None
self
.
_gradient_weights
=
None
def
initialize
(
self
,
weights_initializer
,
bias_initializer
):
weights_shape
=
(
self
.
input_size
,
self
.
output_size
)
self
.
weights
[
0
:
self
.
input_size
,
:]
=
weights_initializer
.
initialize
(
weights_shape
,
self
.
input_size
,
self
.
output_size
)
bias_shape
=
(
1
,
self
.
output_size
)
self
.
weights
[
self
.
input_size
,
:]
=
bias_initializer
.
initialize
(
bias_shape
,
self
.
input_size
,
self
.
output_size
)
def
forward
(
self
,
input_tensor
):
batch_size
=
input_tensor
.
shape
[
0
]
input_plus_bias
=
np
.
ones
((
batch_size
,
input_tensor
.
shape
[
1
]
+
1
))
#replaced input_size with input cols
input_plus_bias
[:,
0
:
input_tensor
.
shape
[
1
]]
=
input_tensor
#replaced input_size with input cols
self
.
input_tensor
=
input_plus_bias
# updated this call
output
=
np
.
matmul
(
input_plus_bias
,
self
.
weights
)
return
output
@property
def
gradient_weights
(
self
):
return
self
.
_gradient_weights
@gradient_weights.setter
def
gradient_weights
(
self
,
w
):
self
.
_gradient_weights
=
w
# gradient_weights = property(get_gradient_weights, set_gradient_weights)
@property
def
optimizer
(
self
):
return
self
.
_optimizer
@optimizer.setter
def
optimizer
(
self
,
ow
):
self
.
_optimizer
=
ow
# optimizer = property(get_optimizer, set_optimizer)
def
backward
(
self
,
error_tensor
):
self
.
error_tensor
=
np
.
copy
(
error_tensor
)
error_tensor
=
np
.
matmul
(
error_tensor
,
self
.
weights
.
T
)
#enter gradient_tensor in place of error_tensor
grad
=
np
.
matmul
(
self
.
input_tensor
.
T
,
self
.
error_tensor
)
self
.
_gradient_weights
=
grad
if
not
(
self
.
_optimizer
==
None
):
self
.
weights
=
self
.
_optimizer
.
calculate_update
(
self
.
weights
,
grad
)
return
error_tensor
[:,
0
:
error_tensor
.
shape
[
1
]
-
1
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment