我正在处理以下 leetcode 问题:
给定'1'(陆地)和'0'(水)的二维网格图,计算岛屿的数量。岛屿四面环水,由相邻陆地水平或垂直连接而成。您可以假设网格的所有四个边缘都被水包围。
示例 1:
输入:11110 11010 11000 00000
输出:1
示例 2:
输入:11000 11000 00100 00011
输出:3
并写了以下明确的解决方案:
from collections import OrderedDict
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
if len(grid) == 0:
return 0
land = ones(grid)
parcels = len(land)
islands = 0
while parcels > 0:
unexplored = []
start = land.popitem()[0]
parcels = parcels - 1
print(start)
up = (start[0] + 1, start[1])
down = (start[0] - 1, start[1])
left = (start[0], start[1] - 1)
right = (start[0], start[1] + 1)
if up in land:
unexplored.append(up)
del land[up]
if down in land:
unexplored.append(down)
del land[down]
if left in land:
unexplored.append(left)
del land[left]
if right in land:
unexplored.append(right)
del land[right]
while len(unexplored) > 0:
start = unexplored.pop()
parcels = parcels - 1
up = (start[0] + 1, start[1])
down = (start[0] - 1, start[1])
left = (start[0], start[1] - 1)
right = (start[0], start[1] + 1)
if up in land:
unexplored.append(up)
del land[up]
if down in land:
unexplored.append(down)
del land[down]
if left in land:
unexplored.append(left)
del land[left]
if right in land:
unexplored.append(right)
del land[right]
islands += 1
return islands
def ones(grid):
ones = OrderedDict()
height = len(grid)
width = len(grid[0])
for i in range(0,height):
for j in range(0,width):
if grid[i][j] == "1":
ones[(i,j)] = 1
return ones
但是,我的直觉告诉我,在 1 和 0 的网格中找到岛屿的数量应该是一个很好的机器学习问题,与图像检测相关。
因此,我编写了以下代码来生成测试用例:
from random import randint
def make_grid():
width = 30
height = 30
grid = []
for i in range(0,height):
row = []
for j in range(0,width):
if randint(0,1) == 0:
row.append("0")
else:
row.append("1")
grid.append(row)
return grid
然后,我生成了 60,000 个案例,并使用我的显式解决方案对它们进行了评分。我将它们分成 50,000 个训练用例和 10,000 个测试用例。
然后,我尝试将训练案例输入到 Keras 网络中,该网络每层 64 个节点,使用 relu 深 10 层,最后使用 softmax 激活函数。我对模型进行了 250 次迭代训练。
我在识别火车和测试用例的“图像”中的岛屿数量方面得到了低准确度的统计数据。
然后我尝试使用两个卷积层将它们输入 Keras 网络,第一个 2x2 和第二个 4x4,每个有 64 个节点,然后是 2 个密集层,每层有 64 个节点。我对模型进行了 3 次迭代训练。
在预测火车和测试用例的“图像”中的岛屿数量时,我得到了低准确度的统计数据。
这只是神经网络的一个坏问题吗?我是否使用了错误的网络?我的网络是否太小?我需要做更多的测试用例吗?
任何输入表示赞赏!
