def format_size(size): names = ["B", "KB", "MB", "GB", "TB"] for i in range(len(names)): if (size / 1024) < 1: return f"{size:.2f}, {names[i]}" size = size / 1024
时间: 2023-06-27 17:03:21 浏览: 44
这是一个 Python 函数,用于将文件大小转换为可读性更好的格式。它接受一个参数 size,表示文件大小(以字节为单位),并返回一个字符串,表示转换后的大小及其单位。
具体来说,该函数将文件大小按照 1024 的倍数进行转换,并在转换过程中使用一个名为 names 的列表来存储不同的单位(例如,B、KB、MB 等)。然后,它使用一个循环来逐个比较文件大小和 1024 的倍数,并在找到合适的单位后返回转换后的字符串。
例如,如果输入的文件大小为 12345678 字节,该函数将返回字符串 "11.77 MB",其中 MB 表示兆字节。
相关问题
class TimeWindowSequence(Sequence): def __init__(self, x, y, batch_size, window_size): self.x = x self.y = y self.batch_size = batch_size self.window_size = window_size self.window_count = int(np.ceil(x.shape[0] / window_size)) def __len__(self): return int(np.ceil(self.x.shape[0] / self.batch_size)) def __getitem__(self, idx): batch_x = np.zeros((self.batch_size, self.window_size, self.x.shape[1])) batch_y = np.zeros((self.batch_size, self.y.shape[1])) for i in range(self.batch_size): j = idx * self.batch_size + i if j >= self.window_count: break window_x = self.x[j*self.window_size:(j+1)*self.window_size, :] window_y = self.y[j*self.window_size:(j+1)*self.window_size, :] batch_x[i, :window_x.shape[0], :] = window_x batch_y[i, :] = window_y[-1, :] return batch_x, batch_y出现
这段代码中似乎缺少了一个缩进,导致 `return` 语句在 `for` 循环内部。可以尝试将 `return` 语句缩进到 `for` 循环外部,如下所示:
```
class TimeWindowSequence(Sequence):
def __init__(self, x, y, batch_size, window_size):
self.x = x
self.y = y
self.batch_size = batch_size
self.window_size = window_size
self.window_count = int(np.ceil(x.shape[0] / window_size))
def __len__(self):
return int(np.ceil(self.x.shape[0] / self.batch_size))
def __getitem__(self, idx):
batch_x = np.zeros((self.batch_size, self.window_size, self.x.shape[1]))
batch_y = np.zeros((self.batch_size, self.y.shape[1]))
for i in range(self.batch_size):
j = idx * self.batch_size + i
if j >= self.window_count:
break
window_x = self.x[j*self.window_size:(j+1)*self.window_size, :]
window_y = self.y[j*self.window_size:(j+1)*self.window_size, :]
batch_x[i, :window_x.shape[0], :] = window_x
batch_y[i, :] = window_y[-1, :]
return batch_x, batch_y
```
这样应该就可以解决这个问题了。
def accuracy(self, x, t, batch_size=100): if t.ndim != 1 : t = np.argmax(t, axis=1) acc = 0.0 for i in range(int(x.shape[0] / batch_size)): tx = x[i*batch_size:(i+1)batch_size] tt = t[ibatch_size:(i+1)*batch_size] y = self.predict(tx, train_flg=False) y = np.argmax(y, axis=1) acc += np.sum(y == tt) return acc / x.shape
这段代码中有一个语法错误,导致它无法正常运行。具体来说,第5行的代码中,应该在 `(i+1)` 和 `batch_size` 之间加上一个乘号 `*`,表示乘法运算,否则会出现 `SyntaxError: invalid syntax` 的错误。
修改后的代码应该是这样的:
```python
def accuracy(self, x, t, batch_size=100):
if t.ndim != 1 :
t = np.argmax(t, axis=1)
acc = 0.0
for i in range(int(x.shape[0] / batch_size)):
tx = x[i*batch_size:(i+1)*batch_size]
tt = t[i*batch_size:(i+1)*batch_size]
y = self.predict(tx, train_flg=False)
y = np.argmax(y, axis=1)
acc += np.sum(y == tt)
return acc / x.shape[0]
```
这样修改后,这段代码应该能够正常运行了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)